在WebKit中使用本机代码函数作为JavaScript对象 [英] Using native code functions as JavaScript objects in WebKit

查看:74
本文介绍了在WebKit中使用本机代码函数作为JavaScript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在基于WebKit的浏览器中将本机代码函数用作JavaScript对象。是不是可以直接别名这些功能?

I'm unable to use native code functions as JavaScript objects in WebKit-based browsers. Is it impossible to alias these functions directly?

这是最容易通过示例解释的,所以这是我在Developer Tools控制台中运行的内容:

This is easiest to explain by example, so here is what I am running in the Developer Tools console:

console.warn;
// Outputs:
// function warn() {
//     [native code]
// }

console.warn("console.warn");
// Outputs: "console.warn"

var _c = console;
_c.warn("_c.warn");
// Outputs: "_c.warn"

var _w = console.warn;
_w("_w");
// Outputs: "TypeError: Type error" on Safari/WebKit (Mac)
// Outputs: "TypeError: Illegal invocation" on Chrome (Mac)

var _w2 = function () { console.warn.apply(console, arguments); }
_w2("_w2");
// Outputs: "w2"






当我尝试在Safari中使用 jQuery Lint 时出现了这个问题;如果window.console不存在,它使用以下方法来防止破坏:


This issue came up as I tried to use jQuery Lint in Safari; it uses the following approach to prevent breakage if window.console does not exist:

_console = {
    warn: window.console && console.warn || function(){},
    ...
}

_console.warn("some error");

这是我的临时解决方法:

Here's my temporary workaround:

if((jQuery.browser.safari || jQuery.browser.webkit) && window.console) {
    jQuery.LINT.level = 3;
    jQuery.LINT.console = {
        warn: function() { console.warn.apply(console, arguments); },
        group: function() { console.group.apply(console, arguments); },
        groupEnd: function() { console.groupEnd(); },
        groupCollapsed: function() { console.group.apply(console, arguments); },
        log: function() { console.log.apply(console, arguments); }
    }
}


推荐答案

您不能在JavaScript中替换任何方法,无论是否为本机,WebKit或不是。

You can't alias any methods in JavaScript, native or not, WebKit or not.

当您说 var时_w = console.warn; ,你正在剥离警告远离其所有者对象 console 并将其视为一个独立的功能。当你调用它时,它没有这个控制台的引用,所以它无法工作。

When you say var _w = console.warn;, you are stripping warn away from its owner object console and treating it as a standalone function. When you call it, it gets no this reference to the console, so it fails to work.

您可能会发现绑定方法在其他语言中的工作方式有所不同,但这就是JavaScript方法调用的设计方式: this 引用仅基于所有者 owner.method()(或<$ c $)中的函数传递给函数C>所有者[ '方法']())。如果在没有所有者的情况下单独调用该函数,设置为窗口对象,该方法很可能会下降结束。

You may find this unusual from how bound methods work in other languages, but that's just how JavaScript method calls are designed: the this reference is passed to a function based solely on what owner is in owner.method() (or owner['method']()). If the function is called alone without an owner, this is set to the window object and the method will most likely fall over.

要解决此问题并传入正确的,您必须使用 method.call (或 method.apply )显式地由@slebetman描述,或者使用像<$ c这样的闭包创建自己的绑定函数$ c> var _w = function(){console.warn.apply(console,arguments)}; 或者,在ECMAScript第五版中, method.bind(所有者)

To get around this and pass in a proper this, you must either use method.call (or method.apply) explicitly as described by @slebetman, or make your own bound function using a closure like var _w= function() { console.warn.apply(console, arguments) }; or, in ECMAScript Fifth Edition, method.bind(owner).

这篇关于在WebKit中使用本机代码函数作为JavaScript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆