每個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也會相應更改