1

I've a question regarding a piece of javascript code I'm working on. In another question on stackoverflow I've read that you can do this.method1 from method2 inside an object. However, when doing that 'this' points to the current window object and method1 (naturally) cannot be found.

Please advise me on how to best fix this issue.

The error I get is:

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

The code:

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;
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Friso Kluitenberg
  • 1,157
  • 1
  • 14
  • 34

1 Answers1

3

You can change this.initFB(); to FPPL.initFB(); for this script to work. this is related to a function and you created an object (i.e. FPPL).

If you want to use this in your code, I would go with https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new sample:

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);
MartyIX
  • 27,828
  • 29
  • 136
  • 207