JavaScript prototype備忘

來源:互聯網
上載者:User

標籤:

 __proto__:所有對象都有的屬性。

var obj = {};console.log(obj.__proto__); //{}var obj1 = new Object();console.log(obj1.__proto__); //{}var func = function() {};console.log(func.__proto__); //[Function:Empty]

 

prototype:函數對象中的屬性。

var obj = {};var func = function() {};console.log(func.prototype);  //{}console.log(obj.prototype); //undefined

 

所有對象的root為object,object的__proto__為null

var obj = {};console.log(obj.__proto__); //{}console.log(obj.__proto__.__proto__); //nullvar func = function() {};console.log(func.__proto__); //[Function:Empty]console.log(func.__proto__.__proto__); //{}console.log(func.__proto__.__proto__.__proto__);//null

 

使用建構函式建立的對象中,__proto__為建構函式的prototype指向的對象

var Func = function() {};Func.prototype.name = ‘f1‘;console.log(Func.prototype); //{name:‘f1‘}var obj = new Func();console.log(obj.__proto__); //{name:‘f1‘}console.log(Func.prototype==obj.__proto__); //true

 

實現繼承時,__proto__的指向:dog1 instance->Dog.prototype->Animal.prototype->object->null

function Animal() {}Animal.prototype.name = ‘animal‘;function Dog() {}Dog.prototype = new Animal();Dog.prototype.height = 10;
console.log(Animal.prototype); //{name:‘animal‘}
console.log(Dog.prototype); //{height:10}
var dog1 = new Dog();console.log(dog1); //{}console.log(dog1.__proto__); //{height:10}console.log(dog1.__proto__.__proto__); //{name:‘animal‘}console.log(dog1.__proto__.__proto__.__proto__);//{} object console.log(dog1.__proto__.__proto__.__proto__.__proto__);//null root
console.log(dog1.name); //animal
console.log(dog1.height); //10

 

 額,,剛學JavaScript,暫時就想到這麼多。

 

 

 

  

      

 

JavaScript prototype備忘

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.