typeof new String(“aaa”) === “对象”?一切都是对象,但是有原始类型吗?

发布于 2025-01-01 06:47:58 字数 1217 浏览 0 评论 0 原文

会有很多问题需要澄清,所以我会尝试用数字标记它们,以便更容易回答。

最近我一直在学习 javascript。有一个主题是“一切都是对象”。

  1. 在我的“javascript 解释”中,这意味着一切在其原型链的开头都有“对象”。这是正确的吗?

  2. 但是原始类型(stringnumberbooleannullundefined 又如何呢? )?它们是物体吗?例如,我可以调用 "aaa".length 。这是如何运作的?

  3. 函数是实现 [[Call]] 根据此的对象 。这意味着什么? (我认为这与 fun.call(this, arg1) 有关,但请帮助我理解这一点。

    还有typeof 运算符。我之前已经从 MDN 链接了它,但有些事情令人困惑。

  4. <代码>类型"aaa" === "string" 和 typeof String("aaa") === "string" 这看起来很符合预期,但是 String("aaa") 返回什么? ?我猜它会以某种方式解析输入并从中返回一个字符串原语,这是正确的吗?

  5. 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".

  1. In my "interpretation of javascript" that means everything has the "object" in the beginning of its prototype chain. Is this correct?

  2. 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?

  3. 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.

  4. 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?

  5. 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.

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

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

发布评论

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

评论(2

独享拥抱 2025-01-08 06:47:58

1\ 在我的“javascript 解释”中,这意味着一切在其原型链的开头都有“对象”。这是正确的吗?

是的,也不是。默认情况下,所有对象都继承自Object。使用 ES5 的 Object.create 可以拥有一个不继承自 Object 的对象,但它仍然是一个对象。

2\ 但是原始类型(字符串、数字、布尔值、null、未定义)又如何呢?它们是物体吗?例如,我可以调用“aaa”.length。这是如何运作的?

JavaScript 中一切都是对象是一种误解。基元不是对象,但它们可以转换为对象。当使用 . 运算符时,左操作数将转换为对象(if可能)。

3\ 函数是根据此实现 [[Call]] 的对象。这意味着什么? (我认为这是有关 fun.call(this, arg1) 的事情,但请帮助我理解这一点。

[[Call]] 是 ECMAScript 实现用于将对象标记为函数的内部方法。它与Function.prototype.call没有直接关系,它本身也是一个用[[Call]]标记的函数,请参阅13.2.1 [[Call]]

4\ typeof "aaa" === "string" 和 typeof String("aaa") === "string"。这看起来很符合预期,但是 String("aaa") 返回什么?我猜它会以某种方式解析输入并从中返回一个字符串原语。这是正确的吗?

String(),当不用作构造函数时,将其参数转换为字符串原始。因此 String("aaa")"aaa".toString() 相同。在这种情况下这是多余且不必要的。

5\ typeof new String("aaa") === "object" 什么?请向我解释一下它的原型链。我在哪里以及哪个原型上有“aaa”原始字符串值?它与 typeof String("aaa") 有什么不同?

正如您所期望的,用作构造函数的 String() 返回一个继承自 String() 的对象。 字符串原语字符串对象

每当您对某些事情感到困惑时,几乎所有问题都可以通过阅读规范来回答。为了您的方便,在线提供了该规范的带注释版本

1\ In my "interpretation of javascript" that means everything has the "object" in the beginning of it's prototype chain. Is this correct?

Yes and no. All objects, by default, inherit from Object. It is possible, using ES5's Object.create, to have an object that doesn't inherit from Object, but it's still an object.

2\ 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?

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).

3\ 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.

[[Call]] is an internal method used by the ECMAScript implementation to mark objects as functions. It's not directly related to Function.prototype.call, which itself is also a function marked with [[Call]]. See 13.2.1 [[Call]].

4\ 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?

String(), when not used as a constructor, converts its argument to a string primitive. So String("aaa") is the same as "aaa".toString(). It's redundant and unnecessary in this case.

5\ typeof new String("aaa") === "object" What? Please explain it's 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")?

String() used as a constructor returns an object that inherits from String(), 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.

绻影浮沉 2025-01-08 06:47:58

1\ 在我的“javascript 解释”中,这意味着一切都有
“对象”位于其原型链的开头。这是正确的吗?

Ans:不,正如你在问题2中所说的,还有原始类型。

2\ 但是原始类型(字符串、数字、布尔值、null、
不明确的)?它们是物体吗?例如,我可以调用“aaa”.length。如何
这有效吗?

Ans:不,它们是原始类型,而不是对象。当您调用 "aaa".length 时,JavaScript 会自动将字符串原语包装为 String 对象并调用方法或执行属性查找。

3\ 函数是根据此实现 [[Call]] 的对象。什么
这是什么意思? (我认为这与 fun.call(this,
arg1),但请帮助我理解这一点。

Ans:JavaScript中的每个函数实际上都是一个Function对象。

4\ typeof "aaa" === "string" 和 typeof String("aaa") === "string"。
这看起来很符合预期,但是 String("aaa") 返回什么?我
猜测它以某种方式解析输入并返回一个字符串原语
它。这是正确的吗?

Ans: String("aaa") 返回一个原始字符串。

5\ typeof new String("aaa") === "object" 什么?请解释一下它是
原型链给我。我在哪里以及在哪个原型上有
“aaa”这个原始字符串值?它与 typeof 有什么不同
字符串(“aaa”)?

Ans: new String("aaa") 返回一个 String 对象。

1\ In my "interpretation of javascript" that means everything has the
"object" in the beginning of it's prototype chain. Is this correct?

Ans: No, there are also primitive types as you said in question 2.

2\ 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?

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.

3\ 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.

Ans:Every function in JavaScript is actually a Function object.

4\ 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?

Ans: String("aaa") returns a primitive string.

5\ typeof new String("aaa") === "object" What? Please explain it's
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")?

Ans: new String("aaa") returns a String object.

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