我有这个对象,我想将它的值打印到控制台:
var PageStatistics = function (page) {
var myFunctionName = function ()
{
//some logic of operation
//i want to print to the console the current object state to see if my logic works
console.log(this);
}
}
但是在查看控制台时,我会看到一个包含所有窗口详细信息的“窗口”,而不是PageStatistics的对象。
如何返回PageStatistics的当前实例?
更新:添加了一些实际代码 此代码从Facebook API查询获得响应,以计算有关页面行为的一些统计信息:
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);
for (var i = 0; i < data.length; i++) {
var post = data[i];
var postIds = post["id"].split("_");
var postId = postIds[1];
//TODO add feature for 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 the results have a next page - set the path to take it from 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 is need to send another function my instance to continue to update it
FacebookManager.getPageStatistics(page,this,path);
}
//here i want to check what we have gathered so far.
console.log(this);
}
/**
* Check if a variable exists
* @param variable the variable to check
* @returns {boolean} true if exists, false otherwise
*/
var isVariableExists = function (variable) {
if (typeof variable !== 'undefined')
return true;
return false;
}
}
答案 0 :(得分:1)
我认为你正在寻找一些想法
var PageStatistics = function (page) {
this.myVariableName = '';
var mySecondVariable = '';
this.myFunctionName = function ()
{
//some logic of operation
this.myVariableName = 'something';
mySecondVariable = 'second something';
//now this will return refrence of myVariableName variable and myFunctionName function, not of mySecondVariable
console.log(this);
}
}
var test = new PageStatistics();
test.myFunctionName();
答案 1 :(得分:0)
创建您的函数,然后实例化它。
像这样:
var PageStatistics = function () {
this.myFunctionName = function (){
console.log(this);
}
}
var ps = new PageStatistics;
console.log(ps.myFunctionName); // myFunctionName
ps.myFunctionName.call(ps); // PageStatistics