ECMAScript只支持实现继承,主要是依靠原型链来实现的。
function Father (){
this.property = false;
}
Father.prototype.getFProperty = function(){
return this.property;
}
function Son(){
this.Sonproperty = true;
}
Son.prototype = new Father(); //这个是关键
Son.prototype.getSProperty = function(){
return this.Sonproperty;
}
var my_test = new Son();
alert(my_test.getFProperty()); //false
如果Son那里改成this.property = true;那么会对父类的property进行覆盖,调用getFProperty()的时候则返回true
js的继承就是修改property为一个已经实例化的父类对象
而且要记住,任何给儿子写的方法或者重写的方法,都必须要在继承语句的后面,不然儿子写的方法作废。
而且不要使用对象字面量来添加新的方法,这会使得继承无效
例子:
function Father (){
this.property = false;
}
Father.prototype.getFProperty = function(){
return this.property;
}
function Son(){
this.Sonproperty = true;
}
Son.prototype = new Father(); //这个是关键
Son.prototype= {
getSProperty : function() { return this.Sonproperty},
}
var my_test = new Son();
alert(my_test.getFProperty()); //Error
原型链的问题:
function Father (){
this.array = [1,2,3,4];
}
function Son(){
this.Sonproperty = true;
}
Son.prototype = new Father();
var my_test = new Son();
my_test.array.push(8)
alert(my_test.array); //1,2,3,4,8
var my_test1 = new Son();
alert(my_test1.array) //1,2,3,4,8
引用类型的,虽然在父类的对象属性,但是通过继承之后,它就会变成原型属性
感觉一般都会采用这种继承:
function Father (){
this.array = [1,2,3,4];
}
function Son(){
Father.call(this);
this.name = “lala”;
}
var my_test = new Son();
Son.prototype.getName = function(){return this.name;};
my_test.array.push(8);
alert(my_test.array); //1,2,3,4,8
var my_test1 = new Son();
alert(my_test1.array) //1,2,3,4
alert(my_test1.getName());
这种借用构造函数的方法,可以在Father.call(this,****)这句加上自己喜欢的参数传给父类
我们常用的是这种,继承父类的属性(Father.call(this,name))不用担心引用类型的属性变成共享的属性,然后也继承它的prototype的方法
function Father (Name){
this.array = [1,2,3,4];
this.name = Name;
}
Father.prototype.getName = function(){
return this.name;
};
function Son(Name,age){
Father.call(this,Name);
this.age = 20;
}
Son.prototype = new Father();
var test = new Son(“lala”,18);
alert(test.getName()); //lala
test.array.push(8);
alert(test.array); //1,2,3,4,8
var test1 = new Son(“haha”,20);
alert(test1.array); //1,2,3,4
alert(test1.getName()); //haha
克罗克福德的方法直接让JavaScript创建了一个跟他类似的方法,我们来看看他的方法是怎么样的
function object(o){
function F(){}
F.prototype = o;
var f = new F();
return f;
}
var person ={
name : "lala",
age :18,
array :[1,,2,3,4]
};
var test = object(person);
test.array.push(8);
alert(test.array); //,1,2,3,4,8
var test1 = object(person);
alert(test.array); //1,2,3,4,8
所以Object类创建了一个create方法,方法参数有两个,第一个参数是传入一个对象,第二个参数为这个对象创建它的property属性
obj.hasOwnProperty(‘属性名’) 用于检查给定的属性是否存在于当前实例对象中,(而不是实例原型中)
function Father(){
this.name = "lala";
this.array = [,1,2,3];
}
Father.prototype.sayName = function(){
return this.name;
}
function Son(){
Father.call(this);
this.job = "engineer";
}
Son.prototype = new Father();
Son.prototype.constructor = Son;
Son.prototype.sayJob = function(){
return this.job;
};
var test = new Son();
alert(test.sayName());
这样写好是好,但是Father构造函数用了两次,创建了两份属性,有点浪费空间,最好的是这样做
function isHerit(father,son){
var o = Object.create(father.prototype);
o.constructor = son ;
son.prototype = o;
}
function Father(){
this.name = "lala";
this.array = [,1,2,3];
}
Father.prototype.sayName = function(){
return this.name;
}
function Son(){
Father.call(this);
this.job = "engineer";
}
isHerit(Father,Son);
Son.prototype.sayJob = function(){
return this.job;
};
var test = new Son();
alert(test.sayName());