Inner function cannot access outer functions variable

后端 未结 5 629
攒了一身酷
攒了一身酷 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

    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/

提交回复
热议问题