穷小子

MajorYe
数据加载中……
Prototype的一些新的见解(JS)
对于为什么要使用prototype来实现继承,我就不说了,网上很多
下面主要是我对于prototype的一些见解:
// The constructor function initializes those properties that
// will be different for each instance.
function Rectangle(w, h) {
    this.width = w;
    this.height = h;
}

// The prototype object holds methods and other properties that
// should be shared by each instance.
Rectangle.prototype.area = function( ) { return this.width * this.height; }
var r = new Rectangle(2, 3);
r.hasOwnProperty("width");   // true: width is a direct property of r
r.hasOwnProperty("area");    // false: area is an inherited property of r
"area" in r;                 // true: "area" is a property of r
>>> function pp(){}
>>> pp.prototype.p="33"
"33"
>>> var t=new pp()
>>> tt.p
tt is not defined
[Break on this error] undefined
javascript: with ... (line 1)
>>> t.p
"33"
>>> t.p="44"
"44"
>>> t.p
"44"
>>> var tt=new pp()
>>> tt.p
"33"
>>> pp.prototype.constructor
pp()
>>> tt.constructor
pp()
>>> tt.prototype.constructor
tt.prototype has no properties
[Break on this error] undefined
javascript: with ... (line 1)
>>> tt.prototype.oo="22"
tt.prototype has no properties
[Break on this error] undefined
javascript: with ... (line 1)
>>> "p" in tt
true
>>> tt.hasOwnProperty
hasOwnProperty()
>>> tt.hasOwnProperty("p")
false
>>> tt.tt=0
0
>>> tt.hasOwnProperty("tt")
true
//从我的firebug调试中你能看出来吗?嘿嘿

posted on 2008-04-18 11:25 MajorYe 阅读(185) 评论(0)  编辑  收藏 所属分类: WEB开发


只有注册用户登录后才能发表评论。


网站导航: