12 繼承
?① ECMAScript中描述了原型鏈的概念蕊程,并將原型鏈作為實(shí)現(xiàn)繼承的主要方法。其基本思想是利用原型讓一個(gè)引用類型繼承另一個(gè)引用類型的屬性和方法。
?前面講過:每個(gè)構(gòu)造函數(shù)都有一個(gè)原型對(duì)象,原型對(duì)象都包含一個(gè)指向構(gòu)造函數(shù)的指針收厨,而實(shí)例都包含一個(gè)指向原型對(duì)象的內(nèi)部指針。
function SuperType() {
this.property = true;
}
SuperType.prototype.getSuperValue = function() {
return this.property;
};
function SubType() {
this.subproperty = false;
}
SubType.prototype = new SuperType(); // 繼承了SuperType
SubType.prototype.getSubValue = function() {
return this.subproperty;
};
var instance = new SubType();
alert(instance.getSuperValue()); //true
alert(instance.getSubValue()); //false
原型鏈
?② 原型式繼承:
?③ 組合式繼承:
?④ 寄生式繼承:
?⑤ 寄生組合式繼承: ★
function inheritPrototype(Female,Person){
var protoType=Object.create(Person.prototype);
protoType.constructor=Female;
Female.prototype=protoType;
}
function Person(name){
this.name=name;
}
Person.prototype.sayName=function(){
console.log(this.name+' '+this.gender+' '+this.age);
}
function Female(name,gender,age){
Person.call(this,name);//第一次調(diào)用父類構(gòu)造函數(shù)
this.age=age;
this.gender=gender;
}
inheritPrototype(Female,Person);
Female.prototype.sayAge=function(){
console.log(this.name+' '+this.age);
}
var fm=new Female('skila','female',19);
fm.sayName();//skila female 19
fm.sayAge();skila 19
13 JSON
?① JSON是JS對(duì)象的字符串表示法优构,它使用文本表示一個(gè)JS對(duì)象的信息诵叁,本質(zhì)是個(gè)字符串;
?② JSON的key鍵值對(duì)中的鍵必須帶有""
钦椭。