May
16
1.第一种方式,冒充对象的方式.(利用js里的每一个方法名都是一个Function对象)
//第一种方式,冒充对象的方式.(利用js里的每一个方法名都是一个Function对象)
function Parent(username){
this.username = username;
this.say = function(){
alert(this.username);
}
}
function Child(username,password){
this.temp = Parent;//temp指向Parent所指向的地方 。 利用js里的每一个方法名都是一个Function对象,指向一个方法。
this.temp(username);//初始化方法里的内容
delete this.temp;//temp没有用了。可以直接删除掉.this不可以丢了
//Parent(username);//这样写表面看起来是正确的,其实是错误的。因为只有new出来的对象才有this,所以调用Parent里的this就没有值了
this.password = password;
this.hello = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
parent.say();//zhangsan
var child = new Child("lisi","123456");
child.say();//lisi
child.hello();//123456
function Parent(username){
this.username = username;
this.say = function(){
alert(this.username);
}
}
function Child(username,password){
this.temp = Parent;//temp指向Parent所指向的地方 。 利用js里的每一个方法名都是一个Function对象,指向一个方法。
this.temp(username);//初始化方法里的内容
delete this.temp;//temp没有用了。可以直接删除掉.this不可以丢了
//Parent(username);//这样写表面看起来是正确的,其实是错误的。因为只有new出来的对象才有this,所以调用Parent里的this就没有值了
this.password = password;
this.hello = function(){
alert(this.password);
}
}
var parent = new Parent("zhangsan");
parent.say();//zhangsan
var child = new Child("lisi","123456");
child.say();//lisi
child.hello();//123456
May
15
javascript里构建类主要有4种方式
1.构造方式定义类
2.原型方式定义类
3.构造和原型结合方式创建类
4.动态的原型方式
各有优缺点,具体如下
1.构造方式定义类,优点:多个实例对象不共享类的属性值,缺点:每个实例对象都会产生出一个函数say
1.构造方式定义类
2.原型方式定义类
3.构造和原型结合方式创建类
4.动态的原型方式
各有优缺点,具体如下
1.构造方式定义类,优点:多个实例对象不共享类的属性值,缺点:每个实例对象都会产生出一个函数say




