简体   繁体   English

当我们将基元视为 JavaScript 中的对象时会发生什么?

[英]What happens when we treat primitives as objects in JavaScript?

I am learning JS from javascript.info .我正在从javascript.info学习 JS。 Now I am currently reading about Methods of primitives .现在我正在阅读有关基本方法的方法

When we run the following code当我们运行以下代码时

let str = 'hello';
alert( str.toUpperCase() ); // HELLO

Internally the following happens (1) creates a special object (2) copies the value of str variable (3) modifies that copied version (4) returns that copied one without touching the original str variable (5) and finally that special object is destroyed.在内部发生以下情况 (1) 创建一个特殊对象 (2) 复制 str 变量的值 (3) 修改复制的版本 (4) 在不触及原始 str 变量的情况下返回复制的版本 (5) 最后该特殊对象被销毁.

That's what the author said.作者是这么说的。 But when we have something like this但是当我们有这样的事情时

let str = 'Hello';
console.log(str.toUpperCase());          // HELLO
console.log(str.split('l'));             // (3) ["He", "", "o"]
console.log(str.startsWith('h'));        // false
console.log(str.concat(' JavaScript'));  // Hello JavaScript
console.log(str);                        // Hello

I just want to know that, is a special object created everytime we treat a primitive as an object?我只是想知道,每次我们将原始对象视为对象时,是否都会创建一个特殊对象? From the above code, I am thinking that the whole process(creates an object, do some process and destroyed) is done for 4 times (because I called 4 methods).从上面的代码,我认为整个过程(创建一个对象,做一些过程并销毁)做了4次(因为我调用了4个方法)。

Is that true?真的吗?

And also I read this following from it我也从中读到了以下内容

The JavaScript engine highly optimizes this process. JavaScript 引擎高度优化了这个过程。 It may even skip the creation of the extra object at all.它甚至可能完全跳过额外对象的创建。 But it must still adhere to the specification and behave as if it creates one.但是它仍然必须遵守规范并且表现得好像它创建了规范一样。

What does it mean?这是什么意思? That above lines make me more confuse about how many times the whole process is done.以上几行让我更加困惑整个过程完成了多少次。

I just want to know that, is a special object created everytime we treat a primitive as an object?我只是想知道,每次我们将原始对象视为对象时,是否都会创建一个特殊对象? I am thinking that the whole process is done for 4 times, because I called 4 methods.我想整个过程做了4次,因为我调用了4个方法。

Yes, that's true.是的,这是真的。

The JavaScript engine highly optimizes this process. JavaScript 引擎高度优化了这个过程。 It may even skip the creation of the extra object at all.它甚至可能完全跳过额外对象的创建。 But it must still adhere to the specification and behave as if it creates one.但是它仍然必须遵守规范并且表现得好像它创建了规范一样。

What does it mean?这是什么意思?

It means that creating 4 objects is wasteful - one would suffice, and you'd still get the same result (in this case).这意味着创建 4 个对象是一种浪费——一个就足够了,而且您仍然会得到相同的结果(在这种情况下)。 The specification is written in a "behave as if" manner, it doesn't govern how an implementation of a javascript engine needs to work step by step, but only what observable behavior it needs to have.该规范以“行为好像”的方式编写,它没有规定 javascript 引擎的实现需要如何逐步工作,而只是规定了它需要具有哪些可观察的行为。 If an engine is smart enough to figure out that it can skip one step, such as doing unnecessary copies or creating unused objects, it is allowed to do so as long as the execution result is still the same.如果一个引擎足够聪明,可以发现它可以跳过一个步骤,例如做不必要的副本或创建未使用的对象,只要执行结果仍然相同,就可以这样做。

And for method calls on primitive values, that's a relatively simple optimisation - the engine doesn't need to actually allocate an object in memory, it can just skip ahead to the step where the property is looked up on the hypothetical special object.对于原始值的方法调用,这是一个相对简单的优化——引擎不需要在内存中实际分配一个对象,它可以直接跳到在假设的特殊对象上查找属性的步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JavaScript中将原语包装为对象的开销是多少? - what is the overhead of wrapping primitives as objects in javascript? 当我们用Java语言重新声明对象时会发生什么? - What happens when we do the re-declaration of object in Javascript? 在构造函数变量上定义对象时,JavaScript中会发生什么 - What happens in JavaScript when defining objects on Constructor Variables Javascript对象与原语 - Javascript objects vs primitives JavaScript 中的字符串基元和字符串对象有什么区别? - What is the difference between string primitives and String objects in JavaScript? Javascript 数组是原语吗? 字符串? 对象? - Are Javascript arrays primitives? Strings? Objects? Javascript,当我们为只读数据属性(例如字符串长度)赋值时会发生什么? - Javascript, what happens when we assign value to read-only data property such as string length? JavaScript 如何临时将字符串原语视为字符串构造函数的实例? - How does JavaScript temporarily treat string primitives as instances of String constructor? 函数创建和返回对象时会发生什么? - What happens when objects are created and returned by a function? 在javascript中产生承诺会发生什么? - What happens when promise is yielded in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM