This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                7年前关闭。
            
        

我只是在学习JavaScript,它有一个叫做Methods的方法:

这样的示例我希望它能起作用,但是我用FireFox编写了它,但它什么也没做:

var myObject = {
 value: 0,
 increment: function (inc) {
   this.value += inc;
   }
};

console.writeln(myObject.value);
var x = myObject.increment(2);
console.writeln(x);


怎么了?

最佳答案

1)使用console.log代替console.writeln

2)您必须从函数中返回。如果不这样做,获取值的唯一方法是请求值

var myObject = {
    value: 0,
    increment: function (inc) {
        return this.value += inc;
    }
};

10-07 12:49
查看更多