我有一個對象,我從中調用一個函數。而不是它返回函數本身的值。這可能是重複的,但我找不到合適的解決方案。所以任何關於這個問題的流行詞都會受到高度讚賞。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();
預先感謝您對我們的支持。
嘛'的console.log(this.getWindowWidth);'返回函數' console.log(this.getWindowWidth());'評估它並返回結果,如果這就是你問的(這不是很清楚)。 –
「我有一個我稱之爲函數的對象」 - 不,你不知道。你有一個從你console.log一個函數的對象。你必須在指向函數的指針之後加上'()'來實際調用它。 – Quentin