js constructor问题!
来源:互联网 发布:snmp java 无无法获取 编辑:程序博客网 时间:2024/05/07 02:21
function Oa(){
this.gender = '男'; //注意这里
}
Oa.prototype={
fn1:function(){},
fun2:function(){}
}
function Ob(name,age){
this.name=name;
this.age=age;
}
var cc = function(){};
cc.prototype = Oa.prototype
var dd = new cc();
Ob.prototype=dd;
console.log(Oa.prototype);//
console.log(Ob.prototype);
this.gender = '男'; //注意这里
}
Oa.prototype={
constructor:Oa,//加上的代码
fn1:function(){},
fun2:function(){}
}
function Ob(name,age){
this.name=name;
this.age=age;
}
var cc = function(){};
cc.prototype = Oa.prototype
var dd = new cc();
Ob.prototype=dd;
--------------------<1>
console.log(Oa.prototype);
console.log(Ob.prototype);
this.gender = '男'; //注意这里
}
Oa.prototype={
fn1:function(){},
fun2:function(){}
}
function Ob(name,age){
this.name=name;
this.age=age;
}
var cc = function(){};
cc.prototype = Oa.prototype
var dd = new cc();
Ob.prototype=dd;
console.log(Oa.prototype);//
console.log(Ob.prototype);
function Oa(){
this.gender = '男'; //注意这里
}
Oa.prototype={
constructor:Oa,//加上的代码
fn1:function(){},
fun2:function(){}
}
function Ob(name,age){
this.name=name;
this.age=age;
}
var cc = function(){};
cc.prototype = Oa.prototype
var dd = new cc();
Ob.prototype=dd;
--------------------<1>
console.log(Oa.prototype);
console.log(Ob.prototype);
打印:
Oa.prototype重写了constructor属性之后,它的原型constructor指向了自己的构造函数,而不是一开始object,Ob的原型指向了Oa而不是
一开始的object,
所以如果创建子类可以在--<1>处加上Ob.prototype.constructor =Ob;
这是js创建子类的关键,如果不这样做,原型对象仅仅是一个普通的对象,它只继承自Object.prototype,
这意味着你的类和所有的子类一样是Object的子类
0 0
- js constructor问题!
- JS constructor
- JS constructor
- JS constructor
- js constructor
- js constructor属性
- js constructor和prototype
- JS constructor属性
- js constructor 和 instanceof
- js中的prototype与constructor
- js中的prototype和constructor
- JS Prototype constructor 转载之一
- js中的constructor 和prototype
- js 中的this,constructor ,prototype
- js中的constructor和prototype
- js 类与constructor属性
- js中的constructor与prototype
- js中typeof和constructor
- 黑马程序员——集合总结
- hdu-2199 Can you solve this equation?
- HDU4972 A simple dynamic programming problem(找规律)
- 基于UDP的windows Socket编程
- hdoj 1551 Cable master (二分法)
- js constructor问题!
- Algorithms—223.Rectangle Area
- iBatis开发详解(1)----------开发第一个程序
- sqlite3如何退出...>状态
- 深度学习在图像分类中的应用
- Unity3d打开文件选择对话框
- 1sting
- NSString函数使用
- SPI、I2C、UART三种串行总线协议的区别