2012-12-04 50 views
0

错误:无法将undefined转换为对象:this.page[1]=100;。它已经被定义,最新的错误? enter image description here无法将未定义的对象转换为对象?

var sheepclass ; 
(function($) { 
    sheepclass = function(handler){ 
     var $div = $('div');    
     this.handler = $.extend({ 
      'sizes': 'thin', 
      'eat': 'grass', 
      'color': 'white', 
      'page':[], 
      myalert: function() { 
       myconsole(); 
       function myconsole() { 
        this.page[0] = 100; 
        console.log(this.page[0]); 
       } 
      }, 
      myalert2: function() { 
       this.myalert(); 
      } 
     },handler); 
    } 
})(jQuery); 

$(document).ready(function(){ 
    var blacksheep = new sheepclass({'color':'black'}); 
    blacksheep.handler.myalert2(); 
}) 
+0

为什么你需要将它包装在一个文件准备好处理程序中?您的代码涉及零DOM操作。这么多代码在这里闻起来。 –

+0

了解'this'的范围的经典问题' –

+0

'myalert'部分看起来很狡猾。 为什么要在那里添加一个执行函数的函数,然后初始化该函数? – Cerbrus

回答

0

做出that变量this

var sheepclass ; 
    (function($) { 
     sheepclass = function(handler){ 
      var $div = $('div');    
      this.handler = $.extend({ 
       'sizes': 'thin', 
       'eat': 'grass', 
       'color': 'white', 
       'page':[200,300], 
       myalert: function() { 
        var that = this; 
        myconsole(); 
        function myconsole() { 
         that.page = that.page || [] 
         that.page[0] = 100; 
         console.log(that.page[0]); 
        } 
       }, 
       myalert2: function() { 
        this.myalert(); 
       } 
      },handler); 
     } 
    })(jQuery); 

    $(document).ready(function(){ 
     var blacksheep = new sheepclass({'color':'black'}); 
     blacksheep.handler.myalert2(); 
    }) 
+0

是的,这很好 – FatDogMark

1

试试这个,通过使用that辅助变数

var sheepclass ; 
(function($) { 
    sheepclass = function(handler){ 
     var $div = $('div'); 
     var that = this; 
     this.handler = $.extend({ 
      'sizes': 'thin', 
      'eat': 'grass', 
      'color': 'white', 
      'page':[], 
      myalert: function() { 
       myconsole(); 
       function myconsole() { 
        that.handler.page[0] = 100; 
        console.log(that.handler.page[0]); 
       } 
      }, 
      myalert2: function() { 
       this.myalert(); 
      } 
     },handler); 
    } 
})(jQuery); 

$(document).ready(function(){ 
    var blacksheep = new sheepclass({'color':'black'}); 
    blacksheep.handler.myalert2(); 
}) 
+0

你不需要在那里有闭包变量。你可以在'myalert'中使用'this'。 –

1

里面myconsolethis不等于你的对象传递上下文,而是指Window代替。因此this.pageundefined - 您索引到page的值并没有区别。

你要电话更改为:

myconsole.call(this); 
+0

是的,它的工作,但奇怪的,那么我已经改变了所有我的功能成为* .call(这)...我有很多功能..为什么要使用这些奇怪的方法。它变得奇怪 – FatDogMark

+1

@FatDogMark:目前还不清楚为什么你有很多嵌套函数。当然,你可以将'this'捕获到另一个变量(例如'that')中并且写入'that.page'而不是'this.page',但是这仍然意味着你必须找到对'this'的所有引用并且改变它们到'那个'。没有更简单的方法来做你正在做的事情,这意味着你正在做的不是一个好主意。 – Jon

0

Becouse “本” 是指myconsole功能。

试试这个:

var sheepclass ; 
(function($) { 
    sheepclass = function(handler){ 
     var $div = $('div');  
     **var page = this.page;** 
     this.handler = $.extend({ 
      'sizes': 'thin', 
      'eat': 'grass', 
      'color': 'white', 
      'page':[], 
      myalert: function() { 
       myconsole(); 
       function myconsole() { 
        **page**[0] = 100; 
        console.log(**page**[0]); 
       } 
      }, 
      myalert2: function() { 
       this.myalert(); 
      } 
     },handler); 
    } 
})(jQuery); 
+0

星号是怎么回事? –

+0

似乎没有工作.. – FatDogMark

+0

星号是大胆的风格,对不起。 – ilCrosta

1

很多这样的代码似乎是没有意义的。 document.ready处理程序是不必要的,因为没有DOM操作,因为是IIFE。您的代码可以缩减为:

var sheepclass = function(handler){ 
    this.handler = $.extend({ 
     'sizes': 'thin', 
     'eat': 'grass', 
     'color': 'white', 
     'page':[], 
     myalert: function() { 
      var context = this; 
      function myconsole() { 
       context.page[0] = 100; 
       console.log(context.page[0]); 
      } 
      myconsole(); 
     } 
    },handler); 
} 

var blacksheep = new sheepclass({'color':'black'}); 
blacksheep.handler.myalert(); 

请注意,拥有一个除了调用另一个方法什么都不做的方法是不必要的。