为什么“typeof this”返回“object”?

发布于 2024-10-07 03:47:38 字数 751 浏览 7 评论 0原文

var f = function(o){ return this+":"+o+"::"+(typeof this)+":"+(typeof o) };
f.call( "2", "2" );
// "2:2::object:string"

var f = function(o){ return this+":"+(typeof this)+":"+(typeof o); };
var x = [1,/foo/,"bar",function(){},true,[],{}];
for (var i=0;i<x.length;++i) console.log(f.call(x[i],x[i]));
// "1:object:number"
// "/foo/:object:object"
// "bar:object:string"
// "function () {\n}:function:function"
// "true:object:boolean"
// ":object:object"
// "[object Object]:object:object"

我在 Chrome、Firefox 和 Safari 中看到相同的结果,所以我假设它是按照 规范,但是...为什么?规范中的哪里定义了这个?为什么不针对函数呢?

var f = function(o){ return this+":"+o+"::"+(typeof this)+":"+(typeof o) };
f.call( "2", "2" );
// "2:2::object:string"

var f = function(o){ return this+":"+(typeof this)+":"+(typeof o); };
var x = [1,/foo/,"bar",function(){},true,[],{}];
for (var i=0;i<x.length;++i) console.log(f.call(x[i],x[i]));
// "1:object:number"
// "/foo/:object:object"
// "bar:object:string"
// "function () {\n}:function:function"
// "true:object:boolean"
// ":object:object"
// "[object Object]:object:object"

I see the same results in Chrome, Firefox, and Safari, so I assume it's per the spec, but...why? And where in the spec is this defined? And why not for functions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2024-10-14 03:47:38

正如 ECMA-262 ECMAScript 语言规范第三版中所定义的(参见脚注),它基于 规范(第 15.3.4.4 节):

var result = fun.call(thisArg[, arg1[, arg2[, ...]]]);  

参数

thisArg

确定里面this的值
乐趣。如果 thisArg 为 null 或未定义,
这将是全局对象。
否则,这将等于
Object(thisArg)(如果是,则为 thisArg
thisArg 已经是一个对象,或者
字符串、布尔值或数字(如果 thisArg)
是一个原始值
对应类型)。因此,它是
this == 的类型始终为真
函数执行时为“对象”。

特别注意最后一行。

关键是js原语(stringnumberbooleannullundefined) 是不可变的,因此不能将函数附加到它们。因此,call 函数将原语包装在 Object 中,以便可以附加该函数。

例如:

不起作用:

var test = "string";
//the next 2 lines are invalid, as `test` is a primitive 
test.someFun = function () { alert(this); }; 
test.someFun();

起作用:(

var test = "string";
//wrap test up to give it a mutable wrapper
var temp = Object(test);
temp.someFun = function () { alert(this); };
temp.someFun();

脚注)- 正如 patrick dw 在评论中指出的那样,这将会改变在 ECMA-262 ECMAScript 语言规范第五版 当处于严格模式时:

来自第 15.3.4.4 节:

<块引用>

注意
thisArg 值的传递没有
修改为this值。这
是对第 3 版的更改,其中
未定义或 null thisArg 被替换
与全局对象和 ToObject 是
应用于所有其他值并且
结果作为 this 值传递。

As defined in ECMA-262 ECMAScript Language Specification 3rd edition (see footnote), It's based on the spec (Section 15.3.4.4):

var result = fun.call(thisArg[, arg1[, arg2[, ...]]]);  

Parameters

thisArg

Determines the value of this inside
fun. If thisArg is null or undefined,
this will be the global object.
Otherwise, this will be equal to
Object(thisArg) (which is thisArg if
thisArg is already an object, or a
String, Boolean, or Number if thisArg
is a primitive value of the
corresponding type). Therefore, it is
always true that typeof this ==
"object" when the function executes.

Note in particular the last line.

The crucial thing is that js primitives (string, number, boolean, null, undefined) are immutable, so a function can not be attached to them. Therefore the call function wraps the primitive in an Object so that the function can be attached.

E.g.:

Doesn't work:

var test = "string";
//the next 2 lines are invalid, as `test` is a primitive 
test.someFun = function () { alert(this); }; 
test.someFun();

Works:

var test = "string";
//wrap test up to give it a mutable wrapper
var temp = Object(test);
temp.someFun = function () { alert(this); };
temp.someFun();

(footnote) - as patrick dw noted in the comments, this will change in ECMA-262 ECMAScript Language Specification 5th edition when in strict mode:

From Section 15.3.4.4:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文