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

查看:110
本文介绍了为什么要避免在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中荒谬,它在任何地方都有对象,并且是现存最面向对象的语言之一。但是避免创建基元的对象版本,这是你引用的代码所做的,是有效的。也就是说,避免 new String new Number new 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

字符串,数字和对象的版本booleans主要用于使用为对象类型提供方法的相同机制来提供基元上的方法。当你这样做时

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"

原语字符串创建一个临时对象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天全站免登陆