gpt4 book ai didi

javascript - 在全局范围调用 Object.prototype 方法

转载 作者:数据小太阳 更新时间:2023-10-29 04:37:08 25 4
gpt4 key购买 nike

此代码会引发错误。

try {
alert(hasOwnProperty('window'));
} catch(e) {
alert(e); // Type Error : can't convert undefined to object
}

但是这段代码不会抛出错误。

try {
alert(this.hasOwnProperty('window')); // true (if on browser)
} catch(e) {
// through catch block
alert(e);
}

Live Example | Live Source

据我所知,如果 this 是全局对象,则 func(arg) 等于 this.func(arg)。为什么会发生这样的事情?

最佳答案

认为正在发生的事情是我们有严格模式代码和非严格模式代码的交互。事实上,当我挖出 Firefox 3.6.15 的副本(不支持严格模式)时,我没有使用我发布到你的问题的链接得到错误(它警告“真”两次)。

您显示的代码显然是非严格模式代码。但是浏览器的 hasOwnProperty 实现呢?我怀疑在支持严格模式的浏览器上它是严格的。

当你说

func();

...浏览器所做的是使用标准标识符解析查找 func,然后像您这样做一样调用它:

func.call(undefined);

如果func 是一个松散模式函数,那么在对func 的调用中,this 是全局对象。 但是,如果func 是严格模式函数,则调用中的this未定义

与此相反:

this.func();

...它再次查找 func(这次是通过使用原型(prototype)链的属性解析),然后有效地执行此操作:

this.func.call(this);

在严格模式或松散模式下,这意味着函数中的 this 将是 this。 (当然,在全局范围内,this 是全局对象。)

这是一个使用我们可以看到的代码而不是 hasOwnProperty 的交互示例:

(function() {
"use strict";

window.strictFunction = function() {
display("strictFunction: this === window? " +
(this === window));
display("strictFunction: typeof this: " +
typeof this);
};

})();

strictFunction();
strictFunction.call(undefined);

如您所见,除了在 window 上定义 strictFunction 函数的位之外,这是松散的代码。然后我们从松散的代码中调用该函数两次。结果是这样的:

strictFunction: this === window? falsestrictFunction: typeof this: undefinedstrictFunction: this === window? falsestrictFunction: typeof this: undefined

In contrast, if we do that with a loose function, the result is:

looseFunction: this === window? truelooseFunction: typeof this: objectlooseFunction: this === window? truelooseFunction: typeof this: object

Complete example: Live Copy | Live Source

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Fun With Strict Interactions</title>
<style>
body {
font-family: sans-serif;
}
p {
margin: 0;
}
</style>
</head>
<body>
<script>
(function() {
"use strict";

window.strictFunction = function() {
display("strictFunction: this === window? " +
(this === window));
display("strictFunction: typeof this: " +
typeof this);
};

})();
(function() {

window.looseFunction = function() {
display("looseFunction: this === window? " +
(this === window));
display("looseFunction: typeof this: " +
typeof this);
};

})();

display("Direct call:");
strictFunction();
looseFunction();

display("<hr>Call with <code>.call(undefined)</code>:");
strictFunction.call(undefined);
looseFunction.call(undefined);

display("<hr>Call with <code>.call(window)</code>:");
strictFunction.call(window);
looseFunction.call(window);

function display(msg) {
var p = document.createElement('p');
p.innerHTML = String(msg);
document.body.appendChild(p);
}
</script>
</body>
</html>

输出(使用支持严格模式的JavaScript引擎):

Direct call:strictFunction: this === window? falsestrictFunction: typeof this: undefinedlooseFunction: this === window? truelooseFunction: typeof this: object--Call with .call(undefined):strictFunction: this === window? falsestrictFunction: typeof this: undefinedlooseFunction: this === window? truelooseFunction: typeof this: object--Call with .call(window):strictFunction: this === window? truestrictFunction: typeof this: objectlooseFunction: this === window? truelooseFunction: typeof this: object

关于javascript - 在全局范围调用 Object.prototype 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17500990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com