MooTools的Function.prototype.overloadSetter()有什么作用? [英] What does MooTools' Function.prototype.overloadSetter() do?

查看:103
本文介绍了MooTools的Function.prototype.overloadSetter()有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览MooTools来源以尝试理解其 .implement() .extend()实用程序。

I'm looking through the MooTools source to try and understand its .implement() and .extend() utilities.

每个的定义指的是这样定义的函数:

The definition of each refers to a function defined like this:

var enumerables = true;
for (var i in {toString: 1}) enumerables = null;
if (enumerables) enumerables = ['hasOwnProperty', 'valueOf', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'constructor'];

Function.prototype.overloadSetter = function(usePlural){
    var self = this;
    return function(a, b){
        if (a == null) return this;
        if (usePlural || typeof a != 'string'){
            for (var k in a) self.call(this, k, a[k]);
            if (enumerables) for (var i = enumerables.length; i--;){
                k = enumerables[i];
                if (a.hasOwnProperty(k)) self.call(this, k, a[k]);
            }
        } else {
            self.call(this, a, b);
        }
        return this;
    };
};

然而,我很难理解它的作用。

However, I am having a tough time understanding what it does.

你能解释一下这个函数是如何工作的吗?

Can you explain how this function works and what it does?

推荐答案

overloadSetter

overloadSetter ,加上 overloadGetter ,两个函数装饰器方法。 overloadSetter 函数用于将具有签名 fn(key,value)的函数转换为可以接受对象的函数参数,即: fn({key:value})

overloadSetter, together with overloadGetter, are two function decorator methods. The overloadSetter function is used to transform functions that have the signature fn(key, value) to functions that could accept object arguments, ie: fn({key: value}).

为了做到这一点, overloadSetter 必须包装原始函数。此包装函数具有签名 fn(a,b),这是 fn(键,值)的快捷方式。这实际上成为原始函数的新重载版本。

In order to do this, overloadSetter must wrap the original function. This wrapper function has the signature fn(a, b) which is a shortcut for fn(key, value). This effectively becomes the new overloaded version of the original function.

这个重载函数的第一件事就是检查传递的键是否参数( a )是否为字符串类型。如果它不是字符串,则该函数假定我们正在传递一个对象。因此,它迭代对象中的每个键值对并将原始函数应用于它。另一方面,如果它是一个字符串,它只是将函数应用于 a b 参数的值。

First thing this overloaded function does is check whether the passed key argument (a) is of the string type or not. If it's not a string, the function assumes that we're passing an object. So it iterates over each key-value pair in the object and applies the original function to it. If it's a string, on the other hand, it simply applies the function to the values of the a and b arguments.

示例

为了说明,我们假设我们有以下功能:

To illustrate, let's say we have the following function:

var fnOrig = function(key, value){
    console.log(key + ': ' + value); 
};

var fnOver = fnOrig.overloadSetter();

fnOver('fruit', 'banana');
fnOver({'fruit': 'banana', 'vegetable': 'carrot'});

在第一次调用中, fnOver 函数使用两个参数调用,一个键和一个值。当函数检查 a 参数值的类型时,它会看到它是一个字符串。因此,它只会调用原始的 fnOrig 函数: fnOrig.call(this,'fruit','banana')。我们的控制台输出是'fruit:banana'

In the first invocation, the fnOver function is invoked with two arguments, a key and a value. When the function checks the type of the a argument value, it'll see that it is a string. Therefore, it will simply invoke the original fnOrig function: fnOrig.call(this, 'fruit', 'banana'). Our console output is 'fruit: banana'.

对于第二次调用, fnOver 使用object参数调用函数。由于我们传递了一个对象而不是一个字符串, fnOver 将遍历该对象的成员并调用 fnOrig 函数对于他们每个人。因此,在这种情况下, fnOrig 将被调用两次: fnOrig.call(this,'fruit','banana') fnOrig.call(这是'蔬菜','胡萝卜')。我们的控制台输出是'fruit:banana''vegetable:carrot'

For the second invocation, the fnOver function is invoked with an object argument. Since we passed an object instead of a string, fnOver will iterate through the members of this object and invoke the fnOrig function for each one of them. Thus, fnOrig will be invoked twice in this case: fnOrig.call(this, 'fruit', 'banana') and fnOrig.call(this, 'vegetable', 'carrot'). Our console output is 'fruit: banana' and 'vegetable: carrot'.

附加内容

在包装函数中,您会看到检查<$ c的值$ C> usePlural 。这是 overloadSetter 方法本身的参数。如果将此值设置为 true ,则新函数会将所有参数视为对象。这意味着即使你传递一个字符串键参数,它仍然会被处理为一个对象。

Inside the wrapper function, you'll see that there's an check for the value of usePlural. This is an argument for the overloadSetter method itself. If you set this value to true, the new function will treat all arguments as object. This means that even if you pass a string key argument, it will still be processed as an object.

另一件事, enumerables 排除实际方法声明的代码是因为它解决了某些浏览器的问题,其中本地 Object 方法未在 for / in 循环,即使对象本身实现了它自己的版本。

The other thing, the enumerables code that preludes the actual method declaration, is there because it fixes an issue with some browsers wherein the native Object methods are not enumerated in for/in loops even if the object itself implements its own version of it.

这篇关于MooTools的Function.prototype.overloadSetter()有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆