I'm writing a javascript application that extensively uses a particular form of dataset, like

我正在编写一个广泛使用特定形式的数据集的javascript应用程序,比如

{ foo: 'foo', bar: 'bar', quz: { baz : 'baz' }}

It's only data, but it's nested. There is no static data.

它只是数据,但它是嵌套的。没有静态数据。

I want to create a template for that dataset to keep the code clean. I can either put a template object somewhere and clone that everytime, like (using jquery just for the example)

我想为该数据集创建一个模板,以保持代码清洁。我可以将模板对象放在某处并且每次都克隆它,就像(仅使用jquery作为示例)

var ds = jQuery.extend(true, {}, config.dataset);

or I can create a 'Class' or function prototype that I can call with

或者我可以创建一个我可以调用的“类”或函数原型

var ds = new Dataset();

What performs best ? If using the new constructor, is there a difference between a Class and a function definition ?

什么表现最好?如果使用新的构造函数,Class和函数定义之间是否存在差异?

3 个解决方案

#1


0

The answer to "What performs best?", in general, is: Worry about it if and when you actually have a performance problem.

一般来说,“什么表现最好?”的答案是:如果您确实遇到性能问题,请担心它。

The answer to "What performs best?" in JavaScript is:

“什么表现最好?”的答案在JavaScript中是:

  1. See the general answer, and

    见一般答案,和

  2. It depends, test on your target JavaScript engines (browsers, etc.)

    这取决于,测试你的目标JavaScript引擎(浏览器等)

Let's ignore performance, though, and look at the alternatives you gave.

但是,让我们忽略性能,看看你给出的替代方案。

Implementing a DataSet "class" (constructor and associated prototype, either with the new or old syntax) would be quite complicated and give you virtually no benefit for the use case you describe. You have a nested object structure:

实现DataSet“类”(构造函数和相关原型,使用新的或旧的语法)将非常复杂,并且对于您描述的用例几乎没有任何好处。您有一个嵌套的对象结构:

{ foo: 'foo', bar: 'bar', quz: { baz : 'baz' }}

...and so if you used that as a prototype, it would be very easy to get cross-talk between instances. Example:

...所以如果你把它当作原型,那么在实例之间进行串扰就很容易了。例:

// Demonstrating cross-talk
function DataSet() {
}
DataSet.prototype = { foo: 'foo', bar: 'bar', quz: { baz : 'baz' }};

var d1 = new DataSet();
var d2 = new DataSet();
d1.quz.baz = "updated";
document.body.innerHTML = d2.quz.baz; // "updated" - huh?!

更多相关文章

  1. Python3 函数式编程(高阶函数)
  2. python中range()函数的用法--转载
  3. Python3入门(六)——函数式编程
  4. Python执行系统命令:使用subprocess的Popen函数
  5. python传递列表作为函数参数
  6. python的内置函数
  7. python魔法方法、构造函数、序列与映射、迭代器、生成器
  8. 用python写MapReduce函数——以WordCount为例
  9. Python内置函数介绍

随机推荐

  1. [Android] Android进程与线程基本知识
  2. Android布局优化(五)绘制优化—避免过度绘
  3. 【Android】1:Android APP开发入门篇
  4. QtAndroid详解(3):startActivity实战Andro
  5. Handler机制深入解析
  6. Android实现书籍翻页效果--扩展版
  7. 浅谈Android重力感应
  8. Android动画机制与使用技巧
  9. Android 自动化测试(5)
  10. Android判断网络状态是否断开+Android完