简体   繁体   English

匿名函数的返回值

[英]return value of anonymous function

I want to build a shim. 我想建立一个垫片。 I have the following shim, which excecutes only once and should give the returned value to other functions. 我有以下垫片,该垫片只能执行一次,并且应该将返回值提供给其他函数。 It is a bit like that http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/ . 有点像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;
    }(),

Now I want to use the return value by other functions: 现在,我想通过其他函数使用返回值:

// Uncaught TypeError: Illegal invocation (anonymous function)
if(matches('ul')){
...
}

However I cannot retrieve the return value of matches, because it sa anonymous function. 但是,我无法检索匹配的返回值,因为它是一个匿名函数。

It will only work, if I do not excecute the function at once: 如果我不立即执行该功能,它将只能工作:

// 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')){
...
}

How can I make "WAY 1" work? 如何使“ WAY 1”起作用?

You make it too complex. 您使它太复杂了。 Just do this : 只要这样做:

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;

as you have mentioned, you have a global object named DOM , using it you can do it like: 如前所述,您有一个名为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;
})(),

then call _getMatches whenever you want: 然后在需要时调用_getMatches

DOM._getMatches()

If you have any other global object in your code, you can use it instead of DOM . 如果您的代码中还有其他全局对象,则可以使用它代替DOM

The anonymous function is a function that returns another function. 匿名函数是返回另一个函数的函数。

In the first case, you call it and store the returned function on matches . 在第一种情况下,您将其调用并将返回的函数存储在matches Then you call that returned function later. 然后,您稍后调用该返回的函数。

The problem is that the purpose of that function is to compare the HTML Element stored in this with a selector you pass as an argument. 问题是,该功能的目的是比较存储在HTML元素this与您作为参数传递一个选择。

When you call matches('ul') the value of this is window (or undefined in strict mode). 当你打电话matches('ul')的值, thiswindow (或undefined严格模式)。 That isn't an HTML Element. 那不是HTML元素。

You need something more like: 您需要更多类似的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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