js中的简单问题(静态属性和实例属性)

lsd123 2009-09-29 12:30:25
js中的简单问题(静态属性和实例属性):

function Rectangle(w,h)
{
this.width=w;//问题:this.width是相当与Rectangle.width还是Rectangle.prototype.width
this.height=h;
}

vat rect=new Rectangle(5,10);
如何调用:
Rectangle.width?

rect.width?

还是两者都可以?
...全文
123 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsd123 2009-09-29
  • 打赏
  • 举报
回复
能否说下原理?
youyuf 2009-09-29
  • 打赏
  • 举报
回复

<html>
<head>
<title></title>
<script>
function Rectangle(w,h)
{
this.width=w;//问题:this.width是相当与Rectangle.width还是Rectangle.prototype.width
this.height=h;
}
function test()
{
var rect=new Rectangle(5,10);
alert(rect.height);
}
test();
</script>
</head>
<body>
</body>
</html>
gonghuaqiao 2009-09-29
  • 打赏
  • 举报
回复
rect.width
lsd123 2009-09-29
  • 打赏
  • 举报
回复
js中的简单问题(静态属性和实例属性):

function Rectangle(w,h)
{
this.width=w;//问题:this.width是相当与Rectangle.width还是Rectangle.prototype.width
this.height=h;
}

vat rect=new Rectangle(5,10);
如何调用:
Rectangle.width?

rect.width?

还是两者都可以?
HolyPlace 2009-09-29
  • 打赏
  • 举报
回复
对象的属性相当于对象原型的属性值
lsd123 2009-09-29
  • 打赏
  • 举报
回复
this总是指向调用该函数的实例对象
lsd123 2009-09-29
  • 打赏
  • 举报
回复
function Rectangle(w, h)
{
this.width = w;
// 问题:this.width是相当与Rectangle.width还是Rectangle.prototype.width
this.height = h;
}

那就是说this.width是相当与Rectangle.prototype.width了?

wx23990915 2009-09-29
  • 打赏
  • 举报
回复
rect.width
youyuf 2009-09-29
  • 打赏
  • 举报
回复

<html>

<head>

<title></title>
<script>
function Rectangle(w, h)
{
this.width = w;
// 问题:this.width是相当与Rectangle.width还是Rectangle.prototype.width
this.height = h;
}
function test()
{
var rect = new Rectangle(5, 10);
alert(rect.height);
Rectangle.prototype.area = 50;
alert(rect.area);
}
test();
</script>
</head>

<body>

</body>

</html>

这个你可有看看网上的例子:
http://dev.csdn.net/article/84222.shtm
http://ivanl.javaeye.com/blog/53537

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧