Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- /*prototype introduction:
- 1: all JS object has a default protptype called:
- Object.prototype which is an object has lots of built-in methods: to String:
- */
- /*
- var person = {}
- Object.prototype.name = "david";
- console.log(person.name);
- var p20obj = {}
- console.log(p20obj);
- */
- /*prototype chain! :
- first starts from object itself, if cant find, go to object's prototype,
- if can't find, go to prototype of ocject's prototype, until goes to the ROOT
- Object.prototype, if still can't find, then throw error */
- var person ={
- name : "person",
- age :20
- }
- var davidobj = Object.create(person);
- davidobj.name = "david";
- Object.prototype.name = "object prototype";
- /* console.log(davidobj.name);
- console.log(person.name);
- console.log(Object.prototype.name);
- */
- //Object.getPrototypeOf(obj) : get the prototype of obj
- /*console.log(Object.getPrototypeOf(davidobj) == person) ; //true
- console.log(Object.getPrototypeOf(davidobj) == Object.prototype) ; //false*/
- /*constructor
- when you call the function, it returns you an object*/
- function Person(name, age){
- this.name = name;
- this.age = age;
- }
- var davidObj = new Person("david", 30);
- console.log(davidObj);
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">/*prototype introduction:
- 1: all JS object has a default protptype called:
- Object.prototype which is an object has lots of built-in methods: to String:
- */
- /*
- var person = {}
- Object.prototype.name = "david";
- console.log(person.name);
- var p20obj = {}
- console.log(p20obj);
- */
- /*prototype chain! :
- first starts from object itself, if cant find, go to object's prototype,
- if can't find, go to prototype of ocject's prototype, until goes to the ROOT
- Object.prototype, if still can't find, then throw error */
- var person ={
- name : "person",
- age :20
- }
- var davidobj = Object.create(person);
- davidobj.name = "david";
- Object.prototype.name = "object prototype";
- /* console.log(davidobj.name);
- console.log(person.name);
- console.log(Object.prototype.name);
- */
- //Object.getPrototypeOf(obj) : get the prototype of obj
- /*console.log(Object.getPrototypeOf(davidobj) == person) ; //true
- console.log(Object.getPrototypeOf(davidobj) == Object.prototype) ; //false*/
- /*constructor
- when you call the function, it returns you an object*/
- function Person(name, age){
- this.name = name;
- this.age = age;
- }
- var davidObj = new Person("david", 30);
- console.log(davidObj);
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement