0

1) 为什么在 IE 8 中typeof(window["alert"]) 是 "object" 而不是函数?
2)如何在“window.alert”上调用应用方法?我的意思是我想要做的是:

function exec(method, param)
{
    //because of typeof(window["alert"]) == "object" the actual if looks like typeof(window[method]) == 'function' || method == 'alert'
    if(typeof(window[method]) == 'function')
    {
        window[method].apply(window, [param]);
    }
}

exec("alert","hello");
4

3 回答 3

1
  1. typeof window['alert'] 是一个“函数”...(用 FF 测试)

试试这个代码(typeof而不是typeOf())

function exec(method, param)
{
    if(typeof window[method] == 'function')
    {
        window[method].apply(window, [param]);
    }
}

exec("alert","hello");
于 2012-09-11T11:34:20.863 回答
1

typeof window["alert"]在低于 9 的 Internet Explorer 版本中返回 "object" ,但在 Firefox 中返回 "function" 。我猜这是一个已知问题。下面,这是一篇提到它的文章:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/typeof?redirectlocale=en-US&redirectslug=Core_JavaScript_1.5_Reference%2FOperators%2FSpecial_Operators%2Ftypeof_Operator

于 2012-09-11T11:38:25.217 回答
0
  1. typeof(window["alert"]) 返回“函数”

  2. 你已经写了typeOf。这有效

    function exec(method, param)
    {
        //because of typeof(window["alert"]) == "object" the actual if looks like typeof(window[method]) == 'function' || method == 'alert'
        if(typeof(window[method]) == 'function')
        {
            window[method].apply(window, [param]);
        }
    }
    
    exec("alert","hello");
    
于 2012-09-11T11:34:07.547 回答