0

1) IE 8 でtypeof(window["alert"]) が「オブジェクト」であり、機能しないのはなぜですか?
2) "window.alert" で apply メソッドを呼び出すにはどうすればよいですか? 私がやろうとしていることはこれです:

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

typeof window["alert"]Internet Explorer バージョン 9 未満では "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 に答える
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 に答える
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 に答える