简体   繁体   中英

JavaScript returns function not value

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.

Use parenthesis to call the function, otherwise you have simply captured the function itself.

console.log(this.getWindowWidth());
//                             ^^

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(); 

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(); 

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(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM