__proto__ && prototype

__proto__ && prototypeide

一個對象的__proto__ 屬性和本身的內部屬性[[Prototype]]指向一個相同的值 (一般稱這個值爲原型),原型的值能夠是一個對象值也能夠是null(好比說Object.prototype.__proto__的值就是null)。該屬性可能會引起一些錯誤,由於用戶可能會不知道該屬性的特殊性,而給它賦值,從而改變了這個對象的原型。若是須要訪問一個對象的原型,應該使用方法Object.getPrototypeOf。函數

 

當一個對象被建立時,它的 __proto__ 屬性和內部屬性[[Prototype]]指向了相同的對象 (也就是它的構造函數的prototype屬性)。改變__proto__ 屬性的值同時也會改變內部屬性[[Prototype]]的值,除非該對象是不可擴展的。.net

 

什麼是內部屬性:http://my.oschina.net/xinxingegeya/blog/290167prototype

Be aware that __proto__ is not the same as prototype, since __proto__is a property of the instances (objects), whereas prototype is a property of the constructor functions used to create those objects. code

Consider another object called monkey and use it as a prototype when creating objects with the Human() constructor.對象

var monkey = {
    feeds: 'bananas',
    breathes: 'air'
};
function Human() {
}
Human.prototype = monkey;////使用monkey對象重寫Human的prototype屬性

var developer = new Human();
developer.feeds = 'pizza';
developer.hacks = 'JavaScript';

console.log(developer.hacks);//JavaScript
console.log(developer.feeds);//pizza
console.log(developer.breathes); //air

console.log(developer.__proto__ === monkey); //true

console.log(typeof developer.__proto__); //object
console.log(typeof developer.prototype);//undefined
console.log(typeof developer.constructor.prototype);//object
console.log(typeof developer.constructor); //function

The secret link is exposed in most modern JavaScript environments as the __proto__ property (the word "proto" with two underscores before and two after).blog

Be aware that __proto__is not the same as prototype, since __proto__is a property of the instances (objects), whereas prototype is a property of the constructor functions used to create those objects.ip

=======END=======underscore

相關文章
相關標籤/搜索