2017-05-30 196 views
0

我有一个对象,我从中调用一个函数。而不是它返回函数本身的值。这可能是重复的,但我找不到合适的解决方案。所以任何关于这个问题的流行词都会受到高度赞赏。JavaScript返回函数不值

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

预先感谢您对我们的支持。

+0

嘛'的console.log(this.getWindowWidth);'返回函数' console.log(this.getWindowWidth());'评估它并返回结果,如果这就是你问的(这不是很清楚)。 –

+1

“我有一个我称之为函数的对象” - 不,你不知道。你有一个从你console.log一个函数的对象。你必须在指向函数的指针之后加上'()'来实际调用它。 – Quentin

回答

2

简单地改变你的功能就是这样,this.getWindowWidth()

没有paranthesis它会做实际的函数调用,它不会返回一个值。

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

3

使用圆括号调用的函数,否则你干脆拍摄的功能本身。

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

您正在使用this.getWindowHeight代替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();

0

初始化函数被调用,但其他功能没有这么多。 亚历克斯K.说,你需要改变

console.log(this.getWindowWidth); 
console.log(this.getWindowHeight); 
console.log(typeof(this.getWindowHeight)); 

到:

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