gpt4 book ai didi

javascript - 防止在使用 apply 调用函数期间进行 "this"的类型转换

转载 作者:行者123 更新时间:2023-11-30 23:48:45 24 4
gpt4 key购买 nike

Servus忍者,

这就是 ecmascript 5 规范。 (第 118 页)在 Function.prototype.apply(thisArg, argArray) 部分中说:

NOTE: The thisArg value is passed without modification as the this value. This is a change from Edition 3, where a undefined or null thisArg is replaced with the global object and ToObject is applied to all other values and that result is passed as the this value.

这听起来很有希望,但该规范尚未在任何现代浏览器中实现,因此我们必须处理第三个规范的实现。

我现在的问题是“如何使 typeof 语句变为 TRUE?

var foo = function (arg1, arg2) {
alert(typeof this == "string");
};

foo.apply("bar", ["arg1", "arg2"]);

有什么想法吗?

最佳答案

JavaScript 中有两种类型的字符串。

字符串类型的

"hello"

以及 typeof object 的

new String("hello");

apply 将始终包装您作为对象传递的任何值,因为您需要通过 this 关键字使用它。

关于javascript - 防止在使用 apply 调用函数期间进行 "this"的类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2079105/

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