Backbone.js 模型中的数组本质上是静态的? [英] Arrays in a Backbone.js Model are essentially static?

查看:18
本文介绍了Backbone.js 模型中的数组本质上是静态的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 Backbone.js 模型中的数组本质上是静态变量?

Why are arrays in a Backbone.js Model essentially static variables?

class exports.Content extends Backbone.Model
    tags: []

那么如果我制作几个模型:

then if i make a few models:

contentA = new Content()
contentB = new Content()

并在每个模型的数组中添加一个字符串:

and add one string to each models' array:

contentA.tags.push('hello')
contentB.tags.push('world')

它们都以相同的数组结束:

they both end up with the same array:

contentB.tags // ['hello','world']

但是如果是字符串,那就没问题了:

but if it's a string, then there is no problem:

contentA.name = "c1"
contentB.name = "c2"

contentA.name // "c1"

推荐答案

简答

当您调用 extends 来定义您的对象时,您将新对象的配置作为对象字面量传入.对象是通过引用传递的,extends 函数只将标签数组的引用传递给新的类型定义.

The short answer

When you call extends to define your object, you are passing the new object's configuration in as an object literal. Objects are passed by reference, and the extends function only passes a reference to the tags array in to the new type definition.

正如其他人所指出的,您可以通过将 tags 分配给函数来纠正此问题.这是有效的,因为函数会延迟对 tags 的评估,直到对象被实例化.JavaScript 中没有任何本机可以做到这一点,但 Backbone 本身将 tags 识别为函数或值.

As noted by others, you can correct this by assigning tags to a function. This works because a function delays the evaluation of the tags until the object is instantiated. There's nothing native in JavaScript that does this, but it's Backbone itself that recognizes tags as a function or a value.

尽管您的代码是使用 CoffeeScript 编写的,但这归结为 JavaScript 中一些内容的组合:

In spite of your code being in CoffeeScript, this comes down to a combination of a few things in JavaScript:

  • JavaScript 中没有类
  • 立即评估对象文字
  • JavaScript 对象通过引用传递

在 JavaScript 中,没有类.时期.CoffeeScript 为您提供了类的概念,但实际上,它被编译为没有类的 JavaScript.

In JavaScript, there are no classes. Period. CoffeeScript gives you the notion of a class, but in reality, it gets compiled down to JavaScript which has no classes.

您可以拥有类型和类型定义(构造函数),但不能拥有类.Backbone 提供了一个类的定义,它看起来类似于 Java 的扩展"基于类的继承.不过,它仍然只是 JavaScript,没有类.

You can have types and type definitions (constructor functions) but not classes. Backbone provides a class-like definition, that looks similar to Java's "extend" class-based inheritance. It's still just JavaScript, though, which has no classes.

相反,我们拥有的是传递给 extends 方法的对象字面量.就好像你写了这段代码:

What we have, instead, is an object literal being passed in to the extends method. It's as if you write this code:

var config = {标签: []}

var config = { tags: [] }

var MyModel = Backbone.Model.extends(config);

var MyModel = Backbone.Model.extends(config);

在这段代码中,config 是一个对象字面量,或者一个散列,或者一个键/值对,或者一个关联数组.不管你叫它什么名字,它的基本思想都是一样的.您最终会得到一个具有 tags 属性的 config 对象.config.tags 的值是一个空数组,[],它本身就是一个对象.

In this code, config is an object literal, or a hash, or a key/value pair, or an associative array. Whatever name you call it, it's the same basic idea. You end up with a config object that has a tags attribute. The value of config.tags is an empty array, [], which is itself an object.

这让我们回到简短的回答:

Which brings us back to the short answer:

当您调用 extends 来定义您的对象时,您将新对象的配置作为对象字面量传入.对象是通过引用传递的,extends 函数只将标签数组的引用传递给新的类型定义.

When you call extends to define your object, you are passing the new object's configuration in as an object literal. Objects are passed by reference, and the extends function only passes a reference to the tags array in to the new type definition.

正如其他人所指出的,您可以通过将 tags 分配给函数来纠正此问题.这是有效的,因为函数会延迟对 tags 的评估,直到对象被实例化.JavaScript 中没有任何本机可以做到这一点,但 Backbone 本身将 tags 识别为函数或值.

As noted by others, you can correct this by assigning tags to a function. This works because a function delays the evaluation of the tags until the object is instantiated. There's nothing native in JavaScript that does this, but it's Backbone itself that recognizes tags as a function or a value.

这篇关于Backbone.js 模型中的数组本质上是静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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