2017-05-30 248 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();