0

これは私のオブジェクトです:

var obj = {
a:{ x:1 y:function(){return this.x //works fine},
b:{x:2,y:function(){return this.x // instead of returning 2, it's not returning anything}
}

このような名前を付けることに制約はありますか? それぞれが属しているから、それは自分の親ですよね?それでも、なぜ機能しないのですか?

4

1 に答える 1

0

いくつかの構文エラーを削除すると、正常に動作するようです:

http://jsfiddle.net/SBpAF/

var obj = {
     a:{
         x:1,
         y:function(){
             return this.x //works fine
         }},
     b:{
         x:2,
         y:function(){
             return this.x // instead to return 2, not returning anything is it wrong?
         }}
}
console.log(obj.a.y());
console.log(obj.b.y());
于 2012-04-11T06:28:30.567 に答える