继承
function Parent(){//类
this.name = "PPP";
}
方式一:借助原型模拟单一继承
function Child(){//类
this.age = 18;
}
Child.prototype = new Parent();//让子类型的原型指向父类型的实例
var c = new Child();
alert(c.name);
alert(c.age);
alert(c.gender);
方式二:借助this模拟单一继承。该种方式,很容易实现多重继承的。
function Child(){
this.method1 = Parent;//把Parent当做普通函数对象
this.method1();//调用Parent中的代码 this是当前对象 name就被继承过来了
delete this.method1;//已经完成继承,method1就没用了。
}
var c = new Child();
alert(c.name);
方式三:利用动态方法调用完成继承
function A(){
this.name = "AAA";
}
function B(){
this.age = 10;
}
function C(){
A.call(this);//把类A当做普通函数对待,让this执行
B.call(this);
this.gender = "male";
}
var c = new C();
alert(c.name+":"+c.age+":"+c.gender);
问题:在方式三中
function C(){
A.call(this);//把类A当做普通函数对待,让this执行
B.call(this);
this.gender = "male";
}
改成
function C(){
this.A();
this.B();
this.gender = "male";
}
Advanced 继承:
1,写一个函数createClass。通过传入一个json对象,返回一个“类”(别那么较真,这个所谓的类是一个函数,也是一个对象。js中万物皆对象,其实没有类的概念)
function createClass(json){
function F(){}
for(var key in json){
F.prototype[key] = json[key];
}
return F;
}
var Person = createClass({id:123, name:"wer"});
var p = new Person();
alert(p.id);
2,写一个函数extend。如果传入的是一个json对象,则返回一个类。如果传入的是一个函数和一个json对象,则……
function extend(json, prop){
function F(){}
if(typeof json == "object"){
for(var key in json){
F.prototype[key] = json[key];
}
}
if(typeof json == "function"){
F.prototype = json.prototype;
for(var key in prop){
F.prototype[key] = prop[key];
}
}
return F;
}
var Person = extend({id:123, name:"wer"});
var p = new Person();
alert(p.id);
var SuperPerson = extend(Person,{地址:"北京", age:25});
var sp = new SuperPerson();
alert(sp.地址 + sp.age);
function Parent(){//类
this.name = "PPP";
}
方式一:借助原型模拟单一继承
function Child(){//类
this.age = 18;
}
Child.prototype = new Parent();//让子类型的原型指向父类型的实例
var c = new Child();
alert(c.name);
alert(c.age);
alert(c.gender);
方式二:借助this模拟单一继承。该种方式,很容易实现多重继承的。
function Child(){
this.method1 = Parent;//把Parent当做普通函数对象
this.method1();//调用Parent中的代码 this是当前对象 name就被继承过来了
delete this.method1;//已经完成继承,method1就没用了。
}
var c = new Child();
alert(c.name);
方式三:利用动态方法调用完成继承
function A(){
this.name = "AAA";
}
function B(){
this.age = 10;
}
function C(){
A.call(this);//把类A当做普通函数对待,让this执行
B.call(this);
this.gender = "male";
}
var c = new C();
alert(c.name+":"+c.age+":"+c.gender);
问题:在方式三中
function C(){
A.call(this);//把类A当做普通函数对待,让this执行
B.call(this);
this.gender = "male";
}
改成
function C(){
this.A();
this.B();
this.gender = "male";
}
可以吗?为什么。
这里需要说明的是C中没有A函数,this.A()是空的。
this.A要有效,得像方式二一样,shis.A = A;this.A();delete this.A;。
Advanced 继承:
1,写一个函数createClass。通过传入一个json对象,返回一个“类”(别那么较真,这个所谓的类是一个函数,也是一个对象。js中万物皆对象,其实没有类的概念)
function createClass(json){
function F(){}
for(var key in json){
F.prototype[key] = json[key];
}
return F;
}
var Person = createClass({id:123, name:"wer"});
var p = new Person();
alert(p.id);
2,写一个函数extend。如果传入的是一个json对象,则返回一个类。如果传入的是一个函数和一个json对象,则……
function extend(json, prop){
function F(){}
if(typeof json == "object"){
for(var key in json){
F.prototype[key] = json[key];
}
}
if(typeof json == "function"){
F.prototype = json.prototype;
for(var key in prop){
F.prototype[key] = prop[key];
}
}
return F;
}
var Person = extend({id:123, name:"wer"});
var p = new Person();
alert(p.id);
var SuperPerson = extend(Person,{地址:"北京", age:25});
var sp = new SuperPerson();
alert(sp.地址 + sp.age);