Typeof根据括号进行不同的评估

时间:2013-01-06 14:45:13

标签: javascript typeof

我有以下代码示例:

Application.Polyfills.prototype.prettifyCode = function(enable, js_lib, css_lib) {
    return Modernizr.load([{
        test : enable,
        yep : [ css_lib, js_lib ],
        complete: function() {
            return window.prettyPrint && prettyPrint();
        }
    }]);
};

如果我执行console.log(typeof this.prettifyCode)this引用Application.Polyfills,我会function,但如果我console.log(typeof this.prettifyCode()),我会undefined Application.Polyfills.prototype.callPolyfills = function(array) { for (var i = array.length - 1; i >= 0; i--) { console.log(typeof array[i]); (typeof array[i] === "function") ? array[i].apply(this, [ this ]) : console.log('Index [' + i + ']: Invalid [ Function Required ]') ; }; }; 1}}。有人可以告诉我为什么我会这样做,我怎么能解决它,所以我在两种情况下得到相同的结果,因为大多数时候函数需要参数所以我需要使用括号?

更具体地说,我有一个方法:

this.callPolyfills([
        self.polyfillize([
            {
                test : [Modernizr.localstorage, Modernizr.sessionstorage],
                polyfill : [self.polyfills_url.storage_polyfill_url]
            },
            {
                test : [Modernizr.json],
                polyfill : [self.polyfills_url.json_polyfill_url]
            }
        ]),
        self.prettifyCode(self.prettify, self.libraries_url.google_code_prettyfier.css, self.libraries_url.google_code_prettyfier.js),
        self.consoleAvoidError()
    ]);

上面的方法用于调用放在数组中的所有函数,如下所示:

callPolyfills

对那里未知的所有变量进行抽象,我想看看我在该数组中调用的是否实际上是一个函数,因为我现在已经尝试在undefined方法中进行检查。但它失败了,因为它每次返回{{1}},即使它是一个函数。

3 个答案:

答案 0 :(得分:4)

typeof this.prettifyCode()正在检查this.prettifyCode()返回的值的类型;这是undefined

typeof this.prettifyCode但是,正在检查prettifyCodethis成员的类型是什么;这是一个function

你不应该想要这些是相同的。他们可以相同的唯一方法是this.prettifyCode返回一个函数,但typeof this.prettifyCode()typeof this.prettifyCode都是function的事实是无意义的。

答案 1 :(得分:1)

将此作为新答案添加,因为这两个答案并未完全相关


你的职能是callPolyfills。这就是你传递给callPolyfills的问题(你传递的是你想要调用的函数的结果的数组,而不是函数本身)。< / p>

function foo() {
    return "hi";
};

var bar = foo; // stores a reference to the function foo in `bar`, but doesn't call foo.
var baz = foo(); // calls foo and stores the result of the function ("hi") in baz.

你在做什么(selfthis,并且抛开令人困惑的论点是:)

callPolyfills([
    foo(),
    foo(),
    foo()
]);

......即多次向“{1}”说“嗨”,而不是实际传递给你想要的东西。

你应该传递的是什么;

callPolyfills

这会阻止您指定函数的参数;在这种情况下,最简单的方法是将这些函数包装在匿名函数中。

callPolyfills([
    foo,
    foo,
    foo
]);

...但是我不确定你真正获得

答案 2 :(得分:1)

也许不是在callPolyfills中明确调用你的polyfill,你可以改变你的方法吗?像这样:

this.callPolyfills([
    [self.polyfillize, [
        {
            test : [Modernizr.localstorage, Modernizr.sessionstorage],
            polyfill : [self.polyfills_url.storage_polyfill_url]
        },
        {
            test : [Modernizr.json],
            polyfill : [self.polyfills_url.json_polyfill_url]
        }
    ]],
    [self.prettifyCodeself.prettify, self.libraries_url.google_code_prettyfier.css, self.libraries_url.google_code_prettyfier.js],
    self.consoleAvoidError
]);

那么你会这样检查:

Application.Polyfills.prototype.callPolyfills = function(array) {
    for (var i = array.length - 1; i >= 0; i--) {
        if (typeof array[i] == "function") {
            // call with no arguments
            array[i].apply(this);
        }
        else if (typeof array[i].shift() == "function") {
            // call with arguments
            array[i].apply(this, array[i]);
        }
        else {
            // Invalid [ Function Required ]') ;
        }
    };
};