javascript new后的constructor属性
js对象生成时:
如:function BB(a){
this.a="kkk"
}
var b=new BB();
这时b是对象有了BB的的属性prototype所指向的prototype对象;
prototype对象有constructor属性指向BB这个函数;
所以alert(b.constructor==BB.prototype.constructor) //true
这里的“有了”的执行过程是先查看b有没有此属性让后去查看prototype里的属性值,不是简单的A=B:
如添加:b.constructor="ccc";
执行:alert(b.constructor==BB.prototype.constructor) //false; BB.prototype.constructor仍然是BB函数;
看一下taobao的kissy的继承:
O = function (o) {
function F() {
}F.prototype = o;
return new F();
},
sp = s.prototype,
rp = O(sp);r.prototype = rp;
//alert(r.prototype.constructor==sp.constructor)
rp.constructor = r;
//alert(r.prototype.constructor==sp.constructor)
r.superclass = sp;
刚开始理解错了,不明白一直这样来回空调用
javascript new fun的执行过程
(1)创建一个新的对象,并让this指针指向它;(2)将函数的prototype对象的所有成员都赋给这个新对象;(3)执行函数体,对这个对象进行初始化操作
javascript中最常用的继承模式 组合继承
scripttype="text/javascript"//创建基类functionPerson(name,age){this.name=name;this.age=age;}//通过原型方式给基类添加函数(这样可以服用此函数)Person.prototype.showName=func
javascript最常用与实用的创建类的代码
//以构造函数方式添加私有属性和方法functionPerson(name,age,address){this.name=name;this.age=age;this.address=address;}//以原型方式添加公有属性、方法Person.prototype={const
标签:函数,对象,属性,方式,原型