javascript的原型对象属性问题。

[复制链接]
查看11 | 回复3 | 2009-7-15 11:35:07 | 显示全部楼层 |阅读模式
看不懂其中一行:
// 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
为什么说“r.hasOwnProperty("area");
// false: area is an inherited property of r”,会返回一个false值?
二楼的大虾,说说由于prototype的理由撒。

回复

使用道具 举报

千问 | 2009-7-15 11:35:07 | 显示全部楼层
hasOwnProperty,他返回的是一个布尔值,指出一个对象是否具有指定的属性(注意啦,兄弟,这里说的是属性,而非方法),而你那里的AREA是方法,而非属性,当然返回的是FALSE喽!楼上说的与prototype根本着不着边!...
回复

使用道具 举报

千问 | 2009-7-15 11:35:07 | 显示全部楼层
上铺的兄弟说的似乎有道理......
回复

使用道具 举报

千问 | 2009-7-15 11:35:07 | 显示全部楼层
因为prototype...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行