为什么要避免在JavaScript中创建基元对象? [英] Why to avoid creating objects of primitives in JavaScript?

查看:56
本文介绍了为什么要避免在JavaScript中创建基元对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注W3Schools上的JavaScript教程.在阅读几乎每个页面时,它们都会提醒用户避免创建对象",而应使用原始数据类型.他们给出了原因,因为如果使用对象,代码将变得难以理解或执行速度将降低".确实应该避免在JavaScript中创建对象吗?

I am following a JavaScript tutorial on W3Schools. While reading almost on each page they give note to user to "Avoid creating objects" and to use primitive data types instead. They give reason for this as "code becomes difficult to understand or execution speed will be decreased if object are used". Is it true that we should avoid creating objects in JavaScript?

例如:

var value = new Number(1);  // Avoid this
var value = 1;              // do something like this instead.

推荐答案

在JavaScript中,避免创建对象"语句本身是荒谬的,JavaScript遍地都是对象,并且是目前存在的最面向对象的语言之一.但是您引用的代码所做的避免创建基元的对象版本"是有效的.也就是说,避免使用new Stringnew Numbernew Boolean.

The statement "avoid creating objects" on its own is absurd in JavaScript, which has objects everywhere and is one of the most object-oriented languages in existence. But "avoid creating object versions of primitives," which is what the code you quote does, is valid. That is, avoid new String, new Number, and new Boolean.

JavaScript具有字符串,数字和布尔值的原始版本和对象版本.几乎没有任何理由显式地创建其中任何一个的对象版本,这样做确实会导致混乱;查看内嵌评论:

JavaScript has both primitive and object versions of strings, numbers, and booleans. There's almost never any reason to create the object version of any of them explicitly, and doing so can indeed lead to confusion; see inline comments:

var s1, s2, n1, n2;

// These are false because with ===, an object is never equal to a non-object
s1 = new String("hi");
s2 = "hi";
console.log(s1 === s2); // false
n1 = new Number(42);
n2 = 42;
console.log(n1 === n2); // also false

// These are false because even with ==, two *different* objects are never equal
// (even if they're equivalent)
s1 = new String("what the...");
s2 = new String("what the...");
console.log(s1 == s2);  // also false
n1 = new Number(42);
n2 = new Number(42);
console.log(n1 == n2);  // also false

在很大程度上存在字符串,数字和布尔值的对象版本,以使使用与向对象类型提供方法相同的机制可以提供基元上的方法.当你做

The object versions of strings, numbers, and booleans largely exist to enable methods on primitives to be provided using the same mechanism that provides methods to object types. When you do

console.log("foo".toUpperCase()); // "FOO"

primitive 字符串"foo"创建一个临时对象,然后从该对象读取toUpperCase属性.由于对象继承自String.prototype,因此它具有toUpperCase,一切都很好.一旦操作完成,临时对象将被丢弃(除非有任何东西引用该临时对象,但是toUpperCase却无济于事,您必须在String.prototype中添加一个方法以按顺序返回该对象)使其保持在周围).

a temporary object is created for the primitive string "foo", and then the toUpperCase property is read from that object. Since the object inherits from String.prototype, it has toUpperCase and all is well. Once the operation is done, the temporary object is thrown away (unless something keeps a reference to it, but nothing does and nothing can with toUpperCase, you'd have to add a method to String.prototype that returned the object in order for it to be kept around).

这篇关于为什么要避免在JavaScript中创建基元对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆