解释为对象的javascript字符串

解释为对象的javascript字符串,javascript,string,Javascript,String,从生产的角度来看,这可能无关紧要,但我想知道为什么会这样。字符串文字将被解释为对象 function fancyCallback(callback) { callback(this); console.log(typeof this); // just to see it really is an object } fancyCallback.call('string here', console.log); 我得打电话 this.toString() 如果我想要预期的输出,则在函

从生产的角度来看,这可能无关紧要,但我想知道为什么会这样。字符串文字将被解释为对象

function fancyCallback(callback) {
  callback(this);
  console.log(typeof this); // just to see it really is an object
}

fancyCallback.call('string here', console.log);
我得打电话

this.toString()
如果我想要预期的输出,则在函数内部。我知道字符串是javascript中的对象(这很可爱),但在一个简单的console.log('abc')中,它们自然地被解释为字符串。为什么呢?这有用吗?请忽略fancyCallback是在全局范围内定义的这一事实

来自:

thisArg

这项服务的价值在于为《寻欢作乐》提供服务。请注意 可能不是该方法看到的实际值:如果该方法是 函数在非严格模式代码中,null和undefined将被替换 对于全局对象,和基本值将被装箱

原语[又名数字/字符串]被放置在容器对象中,因此它的工作方式与您看到的一样

所以它基本上做的是

> var x = "string";
> typeof x
"string"
> var temp = new String(x);
> typeof temp
"object"

我相信这是因为变量定义在js中的方式。
console.log()
只是调用字符串上的toString方法。这个方法返回false:
(function(){return this;})。call('string here')==='string here'
可能对象被以某种方式修改了。@levi我在他的函数中添加了
console.log(this)
,Chrome显示
字符串{0:“s”,1:“t”,2:“r”,3:“i”,4:“n”,5:“g”,6:,7:“h”,8:“e”,9:“r”,10:“e”,长度:11,formatUnicorn:function,truncate:function,splitOnLast:function,contains:function}
@levi这不是真的,请尝试删除函数中的关键字类型,使其显示console.log(此)。它不会在其上调用toString(),从未听说过术语“boxed”!老实说,我更喜欢javascript。谢谢增加了一点解释