Inner function cannot access outer functions variable

后端 未结 5 658
攒了一身酷
攒了一身酷 2021-01-13 10:14

I have created the following jsfiddle which highlights my problem. http://jsfiddle.net/UTG7U/

var ExampleObject = function() {
   var myArray = new Array();         


        
5条回答
  •  春和景丽
    2021-01-13 10:53

    What this is changes with the scope of each function. However, myArray will be visible to inner function. Example:

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

提交回复
热议问题