2014-02-14 73 views
0

我想構建一個墊片。我有以下墊片,它只能執行一次,並應將返回的值賦予其他函數。這有點像http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/匿名函數返回值

// WAY 1 
matches: function(){ 
     return Element.prototype.matchesSelector 
     || Element.prototype.webkitMatchesSelector 
     || Element.prototype.mozMatchesSelector 
     || Element.prototype.msMatchesSelector 
     || Element.prototype.oMatchesSelector 
     || Element.prototype.matches 
     || Element.prototype.webkitMatches 
     || Element.prototype.mozMatches 
     || Element.prototype.msMatches 
     || Element.prototype.oMatches; 
    }(), 

現在我想等功能,使用返回值:

但我不能檢索匹配的返回值,因爲它是個匿名函數。

它只會工作,如果我不excecute功能一次:

// WAY 2 
matches: function(){ 
    return Element.prototype.matchesSelector 
    || Element.prototype.webkitMatchesSelector 
    || Element.prototype.mozMatchesSelector 
    || Element.prototype.msMatchesSelector 
    || Element.prototype.oMatchesSelector 
    || Element.prototype.matches 
    || Element.prototype.webkitMatches 
    || Element.prototype.mozMatches 
    || Element.prototype.msMatches 
    || Element.prototype.oMatches; 
}, 

// Now it works, but the method is executed every time. 
if(matches('ul')){ 
... 
} 

我怎樣才能讓「WAY 1」的工作?

+0

有什麼不清楚的,特別是關於你的'if(matches('ul')){':你如何處理用正確的上下文調用函數? –

+0

我在對象DomUtil {matches:function()); ...}中包裝「匹配」功能。在dom被加載後調用if(DomUtil.matches('ul'))在點擊處理程序中執行。但是,如果我使用WAY 2而不是WAY 1,它纔有效。然而WAY 2每次調用函數時都會執行該函數,這是沒用的,因爲執行後返回的值沒有差異。因此,我想讓「方式1」工作。 – nimo23

回答

1

正如你所提到的,你有一個全局對象命名爲DOM,使用它,你可以不喜歡它:

matches: (DOM._getMatches = function(){ 
    return Element.prototype.matchesSelector 
    || Element.prototype.webkitMatchesSelector 
    || Element.prototype.mozMatchesSelector 
    || Element.prototype.msMatchesSelector 
    || Element.prototype.oMatchesSelector 
    || Element.prototype.matches 
    || Element.prototype.webkitMatches 
    || Element.prototype.mozMatches 
    || Element.prototype.msMatches 
    || Element.prototype.oMatches; 
})(), 

然後調用_getMatches每當你想要:

DOM._getMatches() 

如果您有任何其他全局ob ject在你的代碼中,你可以使用它而不是DOM

+0

問題是該方法不能用作'window'的方法 – Quentin

+0

上面的解決方案有效,但它有一些缺點,我有這樣的東西:var DOM = {}; DOM.util = function( ){}; DOM.util.prototype = {matchesNode:...}。我看到我的Dom.util原型中有matchesNode函數,使用類似這樣的函數是行不通的:matches:(Dom.util.prototype ._getMatches = function(){..} ...我不想使用另一個對象(窗口)來存儲返回的匹配值,匹配應該保留在Dom.util。 – nimo23

+0

這個工作原理:matches:「matches =( DOM._getMatches = function(){..「這不起作用:」matches =(DOM.util._getMatches = function(){..「,這也不起作用」matches =(DOM.util.prot otype._getMatches = function(){..「爲什麼它不起作用? – nimo23

2

你讓它太複雜。只是這樣做:

matches: Element.prototype.matchesSelector 
    || Element.prototype.webkitMatchesSelector 
    || Element.prototype.mozMatchesSelector 
    || Element.prototype.msMatchesSelector 
    || Element.prototype.oMatchesSelector 
    || Element.prototype.matches 
    || Element.prototype.webkitMatches 
    || Element.prototype.mozMatches 
    || Element.prototype.msMatches 
    || Element.prototype.oMatches; 
+0

這確實可以簡化原始代碼,但實際上並沒有解決問題。相同的值被應用於'matches',所以當它被調用時你會得到相同的錯誤。 – Quentin

0

匿名函數是一個函數,返回另一個函數。

在第一種情況下,您調用它並將返回的函數存儲在matches上。然後你稍後再調用返回的函數。

問題是該函數的目的是比較存儲在this中的HTML元素與作爲參數傳遞的選擇器。

當你(在嚴格模式下或undefined)調用matches('ul')thiswindow。這不是一個HTML元素。

你需要更多的東西一樣:

matches.apply(document.getElementById('something_that_might_be_a_list', 'ul'); 
+0

http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/中的墊片層的體系結構與我的相同。不同之處在於,我不將匹配()存儲到窗口或文檔對象。但我想,這不是不工作的原因。 – nimo23

+0

@ nimo23 - 正如我在這個答案中解釋的那樣,在不同類型的對象上存儲'matches'正是爲什麼它不起作用。 – Quentin

+0

Mehran的解決方案確實有效,但只有在匹配函數被調用之前創建該對象時纔有效。這工作:var test = {};匹配:(test._getMatches = function(){..但是,我想要將該方法存儲在原型函數中:DOM.util.prototype._getMatches = function(){..「不幸的是,這不起作用。 – nimo23