javascript--prototype

來源:互聯網
上載者:User

每個javascript函數自動prototype屬性,使用prototype可以為類聲明通用的屬性,當一個對象被建立時,建構函式將會把它的屬性的prototype賦給對象的內部屬性__proto__ 另外,javascript使用prototype實現繼承機制 建立通用屬性  不採用原型時: 複製代碼function Fish(name, color){    this.name = name;    this.color = color;    this.livePlace = "water";    this.canSwim = function(){ console.log("I can swim");};} var cod = new Fish("cod","white");var salmon = new Fish("salmon","black"); console.log(cod.livePlace);   //waterconsole.log(salmon.livePlace); //waterconsole.log(salmon.canSwim == cod.canSwim); //false複製代碼  此時每申明一個執行個體Fish,都會有相同的屬性值livePlace,有些重複,無法共用方法和屬性   使用prototype,執行個體可以共用屬性 複製代碼function Fish(name, color){    this.name = name;    this.color = color;} Fish.prototype.livePlace = "water"; Fish.prototype.canSwim = function(){ console.log("I can swim");}; var cod = new Fish("cod","white");var salmon = new Fish("salmon","black"); console.log(cod.livePlace);console.log(salmon.livePlace);console.log(salmon.canSwim == cod.canSwim); //true複製代碼例子中 執行個體共用屬性livePlace和canSwim屬性      prototype 與 __proto__  1.所有函數的__proto__都指向Function.prototype 2. 對象的__proto__指向 其構造器的prototype   eg: 複製代碼function Fish(name, color){    this.name = name;    this.color = color;} Fish.prototype.livePlace = "water"; Fish.prototype.canSwim = function(){ console.log("I can swim");}; var cod = new Fish("cod","white");var salmon = new Fish("salmon","black");var object = {}; console.log(Fish.__proto__ === Function.prototype); //trueconsole.log(cod.__proto__ === Fish.prototype);      //trueconsole.log(object.__proto__ === Object.prototype); //trueconsole.log(Object.getPrototypeOf(Fish) === Fish.__proto__); //true 複製代碼注意: 瀏覽器不支援getPrototypeOf時可以使用Object.getPrototypeOf 替代  屬性相關方法  hasOwnProperty() :是否含有某屬性 isPrototypeOf() : 是否是..的原型 in    eg: 複製代碼function Fish(name, color){    this.name = name;    this.color = color;} Fish.prototype.livePlace = "water"; Fish.prototype.canSwim = function(){ console.log("I can swim");}; var cod = new Fish("cod","white");var salmon = new Fish("salmon","black");  console.log(Fish.prototype.isPrototypeOf(salmon));  //trueconsole.log(cod.hasOwnProperty("name")); //trueconsole.log("livePlace" in cod);  //true複製代碼   使用prototype實現繼承 方式一:   複製代碼//父類function People(name,age){    this.name = name;    this.age = age;    this.species = "human";    this.getName = function(){        return this.name;    }    this.getAge = function(){        return this.age;    }    this.sayHello = function(){        console.log("hi,I am the father class");    }} //子類Children.prototype = new People(); console.log(Children.prototype.constructor === People); //true Children.prototype.constructor = Children; var wish = new Children("wish");console.log("the name of wish is:"+wish.getName());  //the name of wish is:wish console.log(wish.sayHello());  //hi,I am the father class 複製代碼注意: 1. prototype對象包含一個contructor屬性,每一個執行個體也有一個constructor屬性,預設調用prototype對象的constructor屬性。Children.prototype = new People();給prototype賦了新的值, 此時Children.prototype.constructor值變為People,因而我們需要修改以保證原型鏈的正確性 2. 只要修改了prototype,一定要將新的prototype的constructor指向原來的建構函式 相關資訊查看:js類型判斷及鴨式辨型    方式二: Children.prototype = People.prototype; Children.prototype.constructor = Children;注意:此種方式下如果 Children.prototype做了更改時,People.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.