Javascript - Object return itself -
this question has answer here:
- how “this” keyword work? 18 answers
i have object , want print value console:
var pagestatistics = function (page) { var myfunctionname = function () { //some logic of operation //i want print console current object state see if logic works console.log(this); } }
but when looking @ console see "window" window's details, instead of object of pagestatistics.
how can return current instance of pagestatistics ?
update: added real code code gets response facebook api query calculate statistics page's behaviour:
var pagestatistics = function (page) { var numberofposts = 0; var numberoflikes = 0; var numberofcomments = 0; var numberofshares = 0; var bestpostid = 0; var bestpostlikes = 0; var bestpostcomments = 0; var bestpostshares = 0; var bestposttotalengagement = 0; this.onfacebookdataloaded = function (posts) { var data = posts["data"]; console.log(posts); (var = 0; < data.length; i++) { var post = data[i]; var postids = post["id"].split("_"); var postid = postids[1]; //todo add feature excluding array of posts numberofposts++; var likes = 0; var comments = 0; var shares = 0; if (isvariableexists(post["likes"]) && isvariableexists(post["likes"]["summary"]) && isvariableexists(post["likes"]["summary"]["total_count"])) likes = post["likes"]["summary"]["total_count"]; if (isvariableexists(post["comments"]) && isvariableexists(post["comments"]["summary"]) && isvariableexists(post["comments"]["summary"]["total_count"])) comments =post["comments"]["summary"]["total_count"]; if (isvariableexists(post["shares"]) && isvariableexists(post["shares"]["count"])) shares = post["shares"]["count"]; numberoflikes += likes; numberofcomments += comments; numberofshares += shares; totalengagement = likes + comments + shares; if (bestposttotalengagement < totalengagement) { bestpostid = post["id"]; bestpostlikes = likes; bestpostcomments = comments; bestpostshares = shares; bestposttotalengagement = totalengagement; } } //if results have next page - set path take there if (isvariableexists(posts["paging"]) && isvariableexists(posts["paging"]["next"])) { var next = posts["paging"]["next"]; var pos = next.indexof("/posts"); var path = page.id + next.substr(pos); //here need send function instance continue update facebookmanager.getpagestatistics(page,this,path); } //here want check have gathered far. console.log(this); } /** * check if variable exists * @param variable variable check * @returns {boolean} true if exists, false otherwise */ var isvariableexists = function (variable) { if (typeof variable !== 'undefined') return true; return false; } }
i think looking somethink
var pagestatistics = function (page) { this.myvariablename = ''; var mysecondvariable = ''; this.myfunctionname = function () { //some logic of operation this.myvariablename = 'something'; mysecondvariable = 'second something'; //now return refrence of myvariablename variable , myfunctionname function, not of mysecondvariable console.log(this); } } var test = new pagestatistics(); test.myfunctionname();
Comments
Post a Comment