简体   繁体   English

内部函数无法访问外部函数变量

[英]Inner function cannot access outer functions variable

I have created the following jsfiddle which highlights my problem. 我创建了以下jsfiddle,突出了我的问题。 http://jsfiddle.net/UTG7U/ http://jsfiddle.net/UTG7U/

var ExampleObject = function() {
   var myArray = new Array();
   this.example = function() {
       alert(this.myArray);
   };
}

var exampleObj = new ExampleObject();
exampleObj.example();​

I am new to JavaScript and trying to create an object, field and a method. 我是JavaScript的新手,并尝试创建一个对象,字段和方法。 I can't get my method to access my field variable. 我无法获取我的方法来访问我的字段变量。

you were trying to access a local variable using this operator which is wrong, so here is the working example 您试图使用此运算符访问本地变量,这是错误的,所以这是工作示例

var ExampleObject = function() {
   var myArray = new Array(1,2,3);
   this.example = function() {
       alert(myArray);
   };
}
var exampleObj = new ExampleObject();
exampleObj.example();​

Link: http://jsfiddle.net/3QN37/ 链接: http//jsfiddle.net/3QN37/

You have confused two types of variables: Local variables and member variables. 您混淆了两种类型的变量:局部变量和成员变量。 var myArray is a local variable. var myArray是一个局部变量。 this.myArray is a member variable. this.myArray是一个成员变量。

Solution using only local variables: 解决方案仅使用局部变量:

var ExampleObject = function() {
   var myArray = new Array(); // create a local variable
   this.example = function() {
       alert(myArray); // access it as a local variable
   };
}

var exampleObj = new ExampleObject();
exampleObj.example();​

Solution using only member variables: 解决方案仅使用成员变量:

var ExampleObject = function() {
   this.myArray = new Array(); // create a member variable
   this.example = function() {
       alert(this.myArray); // access it as a member variable
   };
}

var exampleObj = new ExampleObject();
exampleObj.example();​

You don't need the this.myArray . 你不需要this.myArray Using myArray alone will suffice (and work). 单独使用myArray就足够了(并且可以工作)。

alert(myArray); should work fine I think 我认为应该工作正常

What this is changes with the scope of each function. this是每个功能范围的变化。 However, myArray will be visible to inner function. 但是, myArray对内部函数可见。 Example: 例:

var ExampleObject = function() {
   var myArray = new Array();
   this.example = function() {
       alert(myArray);
   };
}
var exampleObj = new ExampleObject();
exampleObj.example();​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 内部函数更改后无法访问外部函数变量 - Inner function cannot access outer function variable after being changed 为什么这不是允许内部函数访问外部函数变量的闭包? - Why is this not a closure that allows an inner function to access an outer function variable? 如何在JS中嵌套内部函数中访问外部函数变量 - How to access outer function variable in nested inner function in JS 如何使用jQuery从外部函数访问内部函数的变量? - How to access the inner function's variable from outer function with jQuery? 为什么在这种情况下,内部函数无法访问外部变量? - Why in this case the inner function has no access to outer variable? 从内部函数访问外部函数变量 - Access outer function variables from inner function 外部函数返回内部函数,可以从外部函数访问值 - Outer function returns an inner function with access to values from outer function 即使外部函数被多次触发,返回的内部函数如何引用相同的外部函数变量? - How come returned inner functions reference the same outer function variable even when the outer function is fired multiple times? 内部函数变量丢失外部函数的“ this”值 - inner function variable losing 'this' value of outer function Javascript:外部函数中的变量不会被内部函数更改 - Javascript: variable in outer function not changed by inner function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM