简体   繁体   English

为什么大多数JavaScript框架都使用对象文字

[英]Why most of the javascript frameworks use object literals

为什么大多数javascript框架都使用对象常量而不是构造函数或带有原型的构造函数?如果有人在新框架中使用带有原型的构造函数,它将是非标准的。对象常量比带有原型的构造函数有什么优势?

I assume you mean how jQuery automatically defines the jQuery/$ objects? 我假设您是说jQuery如何自动定义jQuery / $对象? simple answer ... it's easier that way. 简单的答案...这样比较容易。 Can you imagine how confusing jQuery would be for new users if they had to include the following two lines in every single page? 您能想象如果新用户必须在每个页面中包含以下两行,那么jQuery对新用户会造成多大的困扰?

var jQuery = new jQuery();
var $ = jQuery;

I think you're talking about using {} vs new Object() and [] vs new Array() . 我认为您正在谈论使用{} vs new Object()[] vs new Array()

The literal version in both cases is often faster (of course this depends on the browser). 两种情况下的文字版本通常都更快(当然,这取决于浏览器)。 You also get the added advantage of being able to define properties in an object or items in an array from the onset. 您还可以获得从一开始就可以定义对象或数组中项目的属性的附加优点。 This makes the code more lightweight and readable. 这使代码更加轻巧和可读。

var o = {
  a: 1,
  b: 2,
  c: 3,
  d: 4
};
// vs.
var o = new Object();
o.a = 1;
o.b = 2;
o.c = 3;
o.d = 4;

var a = [1, 2, 3, 4, 5];
// vs.
var a = new Array();
a.push(1, 2, 3, 4, 5);

Edit : I made performance tests for objects and arrays . 编辑 :我对对象数组进行了性能测试。 For me, object literals were actually a bit slower in Chrome. 对我来说,对象字面量实际上在Chrome中要慢一些。 Array literals were much faster, however. 但是,数组字面量要快得多。 The syntax is still preferred, though. 不过,语法仍然是首选。

It all depends on the need of the programme you're writing and the way you want to maintain the code base. 所有这些都取决于您正在编写的程序的需求以及维护代码库的方式。

When you have objects which get instantiated a lot with shared properties and methods it could be more efficient memorywise to use a constructor and fill its prototype. 当您使用共享的属性和方法实例化大量对象时,使用构造函数并填充其原型可能会更加有效。

When the code is more or less a singleton it is probably more efficient to just make an object and fill it with some methods. 当代码或多或少是单例时,仅创建一个对象并用某些方法填充它可能会更有效。

If you would like more information about "patterns" and what they can mean to you you could check out http://addyosmani.com/resources/essentialjsdesignpatterns/book/ 如果您想了解有关“模式”及其对您的含义的更多信息,请访问http://addyosmani.com/resources/essentialjsdesignpatterns/book/

Or if you like a regular (e-)book i would recommend http://shop.oreilly.com/product/9780596806767.do 或者,如果您喜欢普通(e-)书,我建议您http://shop.oreilly.com/product/9780596806767.do

But if your instantiating the object several times then prototype uses less memory because you're not recreating the entire object with all its methods. 但是,如果您多次实例化该对象,则原型将使用较少的内存,因为您不会使用其所有方法重新创建整个对象。

Prototype is nice because it allows you to create a new method on the fly and have all intances of that object use. 原型很不错,因为它允许您即时创建一个新方法并使用该对象的所有实例。 Syntactically it's ugly and I prefer object literal. 从句法上看这很丑陋,我更喜欢对象文字。 But if you're building a massive app and performance is a big issue then prototype is probably the way forward no? 但是,如果您要构建一个大型应用程序,而性能是一个大问题,那么原型可能是前进的方向吗?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM