var example = function() {
console.log(typeof this);
return this;
};
在嚴格模式:example.call('test') # prints 'string'
Function.prototype.call改變此類型,嚴格模式外;爲什麼?
否則,example.call('test') # prints 'object'
然而,console.log(example.call('test'))
打印test
(如你所期望)
爲什麼Function.call
變化typeof 'test' === 'string'
勢必this
裏面example
?
這不是'call'改變任何事情過去了。這是馬虎的模式,強制「this」值到一個對象(在這裏是一個String對象)。 – Bergi