javascript - 'this' inside a function -


this question has answer here:

i've question regarding piece of javascript code i'm working on. in question on stackoverflow i've read can this.method1 method2 inside object. however, when doing 'this' points current window object , method1 (naturally) cannot found.

please advise me on how best fix issue.

the error is:

typeerror: this.initfb not 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 allow server access 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; 

you can change this.initfb(); fppl.initfb(); script work. this related function , created object (i.e. fppl).

if want use this in code, go 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 () {   // can use here };  var mycar = new car("eagle", "talon tsi", 1993); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -