会有很多问题需要澄清,所以我会尝试用数字标记它们,以便更容易回答。
最近我一直在学习 javascript。有一个主题是“一切都是对象”。
-
在我的“javascript 解释”中,这意味着一切在其原型链的开头都有“对象”。这是正确的吗?
-
但是原始类型(string
、number
、boolean
、null
、undefined 又如何呢? )?它们是物体吗?例如,我可以调用 "aaa".length
。这是如何运作的?
-
函数是实现 [[Call]] 根据此的对象 。这意味着什么? (我认为这与 fun.call(this, arg1) 有关,但请帮助我理解这一点。
还有typeof
运算符。我之前已经从 MDN 链接了它,但有些事情令人困惑。
-
<代码>类型"aaa" === "string" 和 typeof String("aaa") === "string"
这看起来很符合预期,但是 String("aaa") 返回什么? ?我猜它会以某种方式解析输入并从中返回一个字符串原语,这是正确的吗?
-
typeof new String("aaa") === "object"
什么?请给我解释一下它的原型链。我在哪里以及哪个原型上有“aaa”原始字符串值?它与typeof String("aaa")
有何不同?
我已经阅读和观看了很多有关该主题的内容,我认为我需要澄清这些内容。
另外,在您的答案中,如果您链接外部资源,请总结其重要部分,并说明其含义,因为我一直在阅读 ecmascript 规范,而且它们很长:)。
另外,如果 javascript 版本存在差异,也请注明。
There will be a lot of questions needing clarifiction, so I'll try to mark them with numbers, to make it easier to respond to it.
Lately I have been studying javascript a lot. There is a subject about "everything is an object".
-
In my "interpretation of javascript" that means everything has the "object" in the beginning of its prototype chain. Is this correct?
-
But what about the primitive types (string
, number
, boolean
, null
, undefined
)? Are they objects? I can call "aaa".length
for example. How does that work?
-
Functions are objects implementing [[Call]] according to this. What does that mean? (I'm thinking it's something about fun.call(this, arg1), but help me understand this.
Also there is the typeof
operator. I have linked it before from the MDN, but there are things confusing.
-
typeof "aaa" === "string"
and typeof String("aaa") === "string"
. This seems quite expectable, but what does String("aaa") return? I guess it parses the input somehow and returns a string primitive from it. Is this correct?
-
typeof new String("aaa") === "object"
What? Please explain its prototype chain to me. Where and on which prototype do I have the "aaa" primitive string value on this? How does it differ from typeof String("aaa")
?
I have read and watched a lot of things on the subject and I think I need these clarified.
Also in your answers, if you link an external resource, please summarize its important part, and state what it means, because I have been reading through ecmascript specifications and they are quite long :).
Also if there is a difference in versions of javascript please state that too.
发布评论
评论(2)
是的,也不是。默认情况下,所有对象都继承自
Object
。使用 ES5 的Object.create
可以拥有一个不继承自Object
的对象,但它仍然是一个对象。JavaScript 中一切都是对象是一种误解。基元不是对象,但它们可以转换为对象。当使用
.
运算符时,左操作数将转换为对象(if可能)。[[Call]]
是 ECMAScript 实现用于将对象标记为函数的内部方法。它与Function.prototype.call
没有直接关系,它本身也是一个用[[Call]]
标记的函数,请参阅13.2.1 [[Call]]。String()
,当不用作构造函数时,将其参数转换为字符串原始。因此String("aaa")
与"aaa".toString()
相同。在这种情况下这是多余且不必要的。正如您所期望的,用作构造函数的
String()
返回一个继承自String()
的对象。 字符串原语 和 字符串对象。每当您对某些事情感到困惑时,几乎所有问题都可以通过阅读规范来回答。为了您的方便,在线提供了该规范的带注释版本。
Yes and no. All objects, by default, inherit from
Object
. It is possible, using ES5'sObject.create
, to have an object that doesn't inherit fromObject
, but it's still an object.It's a misconception that everything is an object in JavaScript. Primitives aren't objects, but they can be converted to objects. When the
.
operator is used, the left operand is converted to an object (if possible).[[Call]]
is an internal method used by the ECMAScript implementation to mark objects as functions. It's not directly related toFunction.prototype.call
, which itself is also a function marked with[[Call]]
. See 13.2.1 [[Call]].String()
, when not used as a constructor, converts its argument to a string primitive. SoString("aaa")
is the same as"aaa".toString()
. It's redundant and unnecessary in this case.String()
used as a constructor returns an object that inherits fromString()
, as you would expect. There's a difference between a string primitive and a string object.Almost all of your questions can be answered by reading the specification whenever you're confused about something. For your convenience, there's an annotated version of the specification available online.
Ans:不,正如你在问题2中所说的,还有原始类型。
Ans:不,它们是原始类型,而不是对象。当您调用
"aaa".length
时,JavaScript 会自动将字符串原语包装为 String 对象并调用方法或执行属性查找。Ans:JavaScript中的每个函数实际上都是一个Function对象。
Ans:
String("aaa")
返回一个原始字符串。Ans:
new String("aaa")
返回一个 String 对象。Ans: No, there are also primitive types as you said in question 2.
Ans: No, they are primitive types, not objects. When you call
"aaa".length
, JavaScript will automatically wrap the string primitive to String object and call the method or perform the property lookup.Ans:Every function in JavaScript is actually a Function object.
Ans:
String("aaa")
returns a primitive string.Ans:
new String("aaa")
returns a String object.