找出Closure是否提供方法(JavaScript模块模式)

时间:2011-08-15 11:58:16

标签: javascript closures

鉴于以下代码,有没有办法让我检测通过调用fn()返回的闭包是否包含给定的方法而不必执行fn本身?

// Example function which provides a closure with an 'execute' method.
var fn = function () {
    return {
        execute: function () {
        }
    }; 
};

// Test for the presense of an 'execute' method in the function's closure if ("function" === typeof fn().execute) { print("supplied function includes an execute method"); } else { print("supplied function does not include an execute method"); }

谢谢!

1 个答案:

答案 0 :(得分:1)

不,没有。没有它就无法检查返回值。 - Felix Kling