为什么`typeof this`返回“object”? [英] Why does `typeof this` return "object"?

查看:199
本文介绍了为什么`typeof this`返回“object”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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中看到相同的结果,所以我假设它是每个< a href =http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf =nofollow>规范,但是......为什么?这个规范中的定义是什么?为什么不用于函数?

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?

推荐答案

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

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[, ...]]]);  



参数



thisArg


确定
fun里面的值。如果thisArg为null或未定义,则
这将是全局对象。
否则,这将等于
Object(thisArg)(如果
thisArg已经是对象,则为thisArg,如果thisArg $ b,则为
String,Boolean或Number $ b是
对应类型的原始值)。因此,当函数执行时,
始终为true ==
object。

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.

请特别注意最后一行。

关键是js原语( string number boolean null undefined )是不可变的,因此无法将函数附加到它们。因此,调用函数将原语包装在 Object 中,以便可以附加该函数。

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.

例如:

不起作用:

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语言规范第5版在严格模式下:

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


来自Section 15.3.4.4:

From Section 15.3.4.4:


注意
thisArg值在没有
修改的情况下作为此值传递。这个
是对版本3的更改,其中
undefined或null thisArg用全局对象替换
而ToObject是
应用于所有其他值和
结果作为此值传递。

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 this`返回“object”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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