变量“未定义”错误,为什么 [英] variable "is not defined" error, why

查看:106
本文介绍了变量“未定义”错误,为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在js文件中定义了一个对象:

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.value.b;
         return {x: x, y: y};
     }
  }
}

在我调用的另一个js文件中此对象功能:

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:

my index.html:

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

我从 myobj.js 中的firebug收到错误:

I got the error from firebug in myobj.js:


this.value 在$中未定义 b $ b行 this.value.a;

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

为什么? ?

推荐答案

mytest = MyObj.test.startTest

这为您提供了一个没有上下文的功能。如果直接调用此函数,则窗口

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

你想要所有 test.startTest()以便这个 test

这篇关于变量“未定义”错误,为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆