Reputation: 403
I have an object from which I call a function. Instead of the value it returns the function itself. This might be a duplicate but I could not find a proper solution. So any buzzword for this matter would be highly appreciated.
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0];
var asd = {
getWindowWidth: function() {
var x = w.innerWidth || e.clientWidth || g.clientWidth;
return x;
},
getWindowHeight: function() {
var x = (function() {
return w.innerWidth || e.clientWidth || g.clientWidth;
})();
return x;
},
init: function() {
console.log("init fired");
console.log(this.getWindowWidth);
console.log(this.getWindowHeight);
console.log(typeof(this.getWindowHeight));
}
}
asd.init();
Thank you in advance for your support.
Upvotes: 0
Views: 1834
Reputation: 20137
Simply change your function like this, this.getWindowWidth()
without paranthesis
it will do actual function call and it wont return a value.
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0];
var asd = {
getWindowWidth: function() {
var x = w.innerWidth || e.clientWidth || g.clientWidth;
return x;
},
getWindowHeight: function() {
var x = (function() {
return w.innerWidth || e.clientWidth || g.clientWidth;
})();
return x;
},
init: function() {
console.log("init fired");
console.log(this.getWindowWidth());
console.log(this.getWindowHeight());
console.log(typeof(this.getWindowHeight()));
}
}
asd.init();
Upvotes: 2
Reputation: 1030
the init function is invoked, but the other functions not so much. As Alex K. said you need to change
console.log(this.getWindowWidth);
console.log(this.getWindowHeight);
console.log(typeof(this.getWindowHeight));
to:
console.log(this.getWindowWidth());
console.log(this.getWindowHeight());
console.log(typeof(this.getWindowHeight()));
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0];
var asd = {
getWindowWidth: function() {
var x = w.innerWidth || e.clientWidth || g.clientWidth;
return x;
},
getWindowHeight: function() {
var x = (function() {
return w.innerWidth || e.clientWidth || g.clientWidth;
})();
return x;
},
init: function() {
console.log("init fired");
console.log(this.getWindowWidth());
console.log(this.getWindowHeight());
console.log(typeof(this.getWindowHeight()));
}
}
asd.init();
Upvotes: 0
Reputation: 2552
You are using this.getWindowHeight instead of this.getWindowHeight()
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0];
var asd = {
getWindowWidth: function() {
var x = w.innerWidth || e.clientWidth || g.clientWidth;
return x;
},
getWindowHeight: function() {
var x = (function() {
return w.innerWidth || e.clientWidth || g.clientWidth;
})();
return x;
},
init: function() {
console.log("init fired");
console.log(this.getWindowWidth());
console.log(this.getWindowHeight());
console.log(typeof(this.getWindowHeight()));
}
}
asd.init();
Upvotes: 0
Reputation: 64526
Use parenthesis to call the function, otherwise you have simply captured the function itself.
console.log(this.getWindowWidth());
// ^^
Upvotes: 3