3. __proto__
•
internal property, aka [[prototype]] !
•
the prototype of this object.!
•
exists internally, but hidden in some browsers!
•
property search!
•
prototype chain: inheritance, instanceof
7. [[Get]](P)
When the [[Get]] method of O is called with property
name P, the following steps are taken:!
1. If O doesn't have a property with name P, go to step 4.!
2. Get the value of the property.!
3. Return Result(2).!
4. If the [[Prototype]] of O is null, return undefined.!
5. Call the [[Get]] method of [[Prototype]] with property
name P.!
6. Return Result(5).
9. How
1. Create a new native ECMAScript object. !
2. The [[Prototype]] property of the newly constructed
object is set to the Object prototype object. !
3. The [[Class]] property of the newly constructed
object is set to "Object". !
4. The newly constructed object has no [[Value]]
property. !
5. Return the newly created native object.
10. 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function Animal() {!
this.eat = true;!
}!
var cat = new Animal();!
!
console.log(cat.eat);!
Animal.prototype.jump = true;!
console.log(cat.jump);!
!
Animal.prototype = {!
bark: true!
};!
!
var dog = new Animal();!
!
console.log(cat.bark);!
console.log(dog.bark);!
11. 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function Animal() {!
this.eat = true;!
}!
var cat = new Animal();!
!
console.log(cat.eat);!
Animal.prototype.jump = true;!
console.log(cat.jump);!
!
Animal.prototype = {!
bark: true!
};!
!
var dog = new Animal();!
!
console.log(cat.bark);!
console.log(dog.bark);!
Run
22. conclusion
•
search for property along the __proto__ chain!
•
property assign not affect the __proto__ property!
•
__proto__ was created at the moment of the object’s
creation!
•
replacing prototype property of the constructor does
not affect the prototype of already created objects
23. Object.create
•
creates a new object with the specified prototype
object and properties.!
•
Syntax!
!
Object.create(proto [, propertiesObject ])
30. How
Object.create ( O [, Properties] ) # Ⓣ Ⓡ!
1. If Type(O) is not Object or Null throw a TypeError exception.!
2. Let obj be the result of creating a new object as if by the
expression new Object() where Object is the standard builtin constructor with that name!
3. Set the [[Prototype]] internal property of obj to O.!
4. If the argument Properties is present and not undefined, add
own properties to obj as if by calling the standard built-in
function Object.defineProperties with arguments obj and
Properties.!
5. Return obj.
31. instanceof
logic behind obj instanceof F
1. Get obj.__proto__!
2. Compare obj.__proto__ against F.prototype!
3. If no match then set temporarily obj =
obj.__proto__ and repeat step 2 until either match
is found or the chain ends.
32. 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function Animal() {!
this.name = 'wangcai';!
}!
function Dog() {}!
Dog.prototype = new Animal();!
!
var d1 = new Dog();!
!
console.log(d1.name);!
console.log(d1 instanceof Animal);!
!
// change the prototype of Class!
Dog.prototype = {!
name: 'yingcai'!
};!
!
// now instanceof ??!
console.log(d1 instanceof Dog);!
console.log(d1 instanceof Animal);!
console.log(Animal.prototype.__proto__ === Object.prototype);!
33. 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function Animal() {!
this.name = 'wangcai';!
}!
function Dog() {}!
Dog.prototype = new Animal();!
!
var d1 = new Dog();!
!
console.log(d1.name);!
console.log(d1 instanceof Animal);!
!
// change the prototype of Class!
Dog.prototype = {!
name: 'yingcai'!
};!
!
// now instanceof ??!
console.log(d1 instanceof Dog);!
console.log(d1 instanceof Animal);!
console.log(Animal.prototype.__proto__ === Object.prototype);!
Run