【问题标题】:Why's this object not working?为什么这个对象不起作用?
【发布时间】:2013-11-02 10:47:17
【问题描述】:
box = new Object();
box.height = 30;
box.length = 20;
box.both = function(box.height, box.length) {
return box.height * box.length;
}
document.write(box.both(10, 20));
正如标题所说。
首先我创建了一个对象。
根据属性、高度和长度制造。
为每个分配一个值。
做了一个方法
在函数中,我会放置 2 个作为对象属性的参数。
退回了他们的产品。
最后调用给它数值的函数..
为什么这不起作用:(
【问题讨论】:
标签:
javascript
html
arguments
【解决方案1】:
box = new Object();
box.height = 30;
box.length = 20;
box.both = function() {
return box.height * box.length;
}
【解决方案2】:
我想你可能想要这样:
box = new Object();
box.height = 30;
box.length = 20;
box.both = function(height,length){
this.height = height;
this.length = length;
return height*length;
}
document.write(box.both(10,20));
【解决方案3】:
问题是:
box.both=function(box.height,box.length){
box.height 和 box.length 不是函数参数的有效名称。这应该是:
box.both=function(h, l) {
return h * l;
}
但是,您似乎希望获取 当前 框实例的区域。在这种情况下,您不需要任何参数:
box.both=function() {
return this.height * this.length;
}
document.write(box.both());