2016-01-16 51 views
1

我有一个关于我正在处理的一段JavaScript代码的问题。在另一个关于stackoverflow的问题上,我读过,你可以在method2里面做一个this.method1。但是,当这样做'this'指向当前窗口对象和方法1(自然)无法找到。'this'里面的函数

请告诉我如何最好地解决这个问题。

我得到的错误是:

TypeError: this.initFB is not a function 
this.initFB(); 

代码:

var FPPL = { 
    /** Variables */ 
    larvaunched  : false, 

    /** 
    * 
    */ 

    initFB   : function(){ 
     if ((typeof(FB)!= 'undefined')) { 
     FB.init({ 
      xfbml: true,  
      status : true, // check login status 
      cookie : true // enable cookies to allow the server to access the session 
      }); 
     } 

    }, 

    initCode  : function(){ 
     console.log(this); 
     this.initFB(); 
     if (!this.checkForLaunch()) 
      return false; 

     window.setTimeout(this.showFaceBox, lb_l_ret.delay); 
    } 
.... 
} 
window.fbAsyncInit = FPPL.initCode; 
+0

也许这initCode点发挥作用initCode – MrOnlineCoder

回答

3

您可以更改this.initFB();FPPL.initFB();此脚本工作。 this与某个功能相关,并且您创建了一个对象(即FPPL)。

如果你想在你的代码中使用this,我会https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new样品去:

function Car(make, model, year) { 
    this.make = make; 
    this.model = model; 
    this.year = year; 
} 

Car.prototype.doSomething = function() { 
    // you can use this here 
}; 

var mycar = new Car("Eagle", "Talon TSi", 1993);