如何将内置函数作为对象属性返回?

Sto*_*ped 1 javascript

在某些情况下,此代码有效.如果它返回console.log那么对p.out函数的调用就好了.

function Notice(mode) {
    this.debug = mode;

    this.out = (function() {
            if(mode) {
                if(window.console) {
                    return console.log;
                } else {

                    return alert;
                }
            } else {
                return Notice.doNothing;
            }
    })(mode);
}

var p = new Notice('1');
p.out('Kool-aid, OH YEAH!');
Run Code Online (Sandbox Code Playgroud)

但是,当它返回alert(或window.alert)时,我收到一个错误:

Error: uncaught exception: [Exception... "Illegal operation on WrappedNative prototype object"  nsresult: "0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)"  location: "JS frame :: http:// .. :: <TOP_LEVEL> :: line 22"  data: no]
Run Code Online (Sandbox Code Playgroud)

作为一个简单的测试,这有效:

out = (function() { return alert; })();
out('hello dolly');
Run Code Online (Sandbox Code Playgroud)

当它被设置为警报时,如何使obj.out正常运行?

sle*_*man 5

问题是在某些实现中,警报实际上是窗口对象的正确方法.也就是说,警报期望this成为窗口对象.这以最简单的形式说明了问题:

var foo = {alert : alert};

/* this fails because the value of 'this' is foo:
 */
foo.alert('hello'); 

/* but this works because we re-point 'this to the
 * correct object, that is the window object:
 */
foo.alert.call(window,'hello');
Run Code Online (Sandbox Code Playgroud)

因此,解决您的问题的方法是返回一个直接调用alert的函数或绑定窗口以在闭包中发出警报.这样的事情:

function Notice (mode) {
    this.out = function(message) {
        if(mode) {
            if(window.console) {
                console.log(message);
            } else {
                alert(message);
            }
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

或这个:

function Notice (mode) {
    this.out = (function() {
        if(mode) {
            if(window.console) {
                return console.log;
            } else {
                return function (message) {alert(message)};
            }
        }
    })();        
}
Run Code Online (Sandbox Code Playgroud)