0
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));

正如标题所说。

首先,我创建了一个对象。根据属性、高度和长度制造。为每个分配一个值。做了一个方法 BOTH 在函数中,我放了 2 个作为对象属性的参数。退回了他们的产品。最后调用给它数值的函数..

为什么这不起作用:(

4

3 回答 3

4

问题是:

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());
于 2013-10-23T23:03:29.783 回答
1

我想你可能想要这样:

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));
于 2013-10-23T23:05:57.857 回答
0
box = new Object();
box.height = 30;
box.length = 20;

box.both = function() {
    return box.height * box.length;
}
于 2013-10-23T23:08:19.463 回答