开发者

Variable "is not defined" message, why

I have defined a object in a js file:

myobj.js

MyObj={
  
  test: {
     value: {a: 10, b: 7},

     startTest: function(){
         var x = this.value.a;
         var y = this.v开发者_C百科alue.b;
         return {x: x, y: y};
     }
  }
}

In another js file I call this object function:

other.js

mytest = MyObj.test.startTest //assign starTest function to mytest
var a = mytest().x;
var b = mytest().y;

my index.html:

<body>
 <script src="myobj.js"></script>
 <script src="other.js"></script>
</body>

I got the error from Firebug in myobj.js:

"this.value" is not defined in the line "this.value.a;"

Why?


mytest = MyObj.test.startTest

This gives you a function without context. If you call this function directly then this is window.

You want to all test.startTest() so that this is test

An excellent guide on this

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜