Using built-in code functions as JavaScript objects in WebKit

I cannot use native code functions as JavaScript objects in WebKit-based browsers. Is it impossible to directly perform these functions?

This is easiest to explain with an example, so this is what I run 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"

This problem arose when I tried using 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 is my 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); }
    }
}
+3
source share
2 answers

JavaScript, , WebKit .

var _w = console.warn;, warn - console . , this console, .

, , , , JavaScript: this , , owner owner.method() ( owner['method']()). , this window, , , .

this, method.call ( method.apply), @slebetman, , var _w= function() { console.warn.apply(console, arguments) }; , ECMAScript, method.bind(owner).

+7

:

console.warn.apply(console,arguments);

this warn.

+2

Source: https://habr.com/ru/post/1728994/


All Articles