新Backbone.Model vs Backbone.Model.extend()()

时间:2022-09-20 18:57:17

I'm new to JS and Backbone

我对JS和脊梁都不熟悉

What's the difference between these two?

这两者有什么区别?

TestModel = new Backbone.Model({ title: "test title" })
TestModel = Backbone.Model.extend({ title: "test title" })

2 个解决方案

#1


74  

There is a basic difference, which in short can be described as "the difference between the project of a house and the house itself".

有一个基本的区别,简而言之就是“房子的项目和房子本身的区别”。

For expert programmers I would just say that "new Backbone.Model" returns an object instance, but "Backbone.Model.extend" returns a constructor function

对于专业的程序员,我只想说“新主干”。Model“返回一个对象实例,但是”Backbone.Model”。“扩展”返回构造函数

FIRST: a new object (i.e. The house)

首先:一个新的物体(例如房子)

var TestModel = new Backbone.Model({ title: "test title" });

you create a new Object whose structure (methods and variables) have been defined somewhere else. Object can be considered as "all the non-native items" of a language, where for "native item" I mean basic types like integers, characters etc.

您将创建一个新对象,该对象的结构(方法和变量)已在其他地方定义。对象可以被认为是一种语言的“所有非本机项”,其中“本机项”指的是基本类型,如整数、字符等。

In the braces {} you pass the value of some variable or method. This is called, as Tomasz Nurkiewicz previously explained, a constructor, because it allows you to 'construct' a new object whose model has been described elsewhere.

在大括号{}中,传递某个变量或方法的值。正如Tomasz Nurkiewicz先前解释的那样,这被称为构造函数,因为它允许您“构建”一个新对象,该对象的模型已经在其他地方被描述过。

To give you a known example: you write

给你一个众所周知的例子:你写作

var myArray = new Array();

It means that you are creating a new Array, which is a non-native object which has been defined elsewhere. you can also write:

这意味着您正在创建一个新的数组,该数组是在其他地方定义的非本地对象。你也可以写:

var myArray = new Array([1,2,3,4,5]);

And it fills the array with the given numbers.

它用给定的数字填充数组。

SECOND: modify definition of an existing object (i.e. The project of the house)

第二:修改现有对象(即房屋项目)的定义

with

var TestModel = Backbone.Model.extend({ title: "test title" })

you say something very simple to your VM: "the object you give me as default is very nice, but I want to implement more functions/properties". So with the "extend" clause you modify the definition of an object adding or override existing method/properties.

你对你的VM说了一些非常简单的话:“你给我的默认对象非常好,但是我想实现更多的函数/属性”。因此,使用“extend”子句可以修改对象的定义,添加或覆盖现有方法/属性。

Example: a nice example in backbone.js is given by the comparator function of a collection. When you extend the object defining it, "it will be used to maintain the collection in sorted order".

示例:主干中的一个很好的示例。js是由一个集合的比较器函数给出的。当您扩展定义它的对象时,“它将用于以排序的顺序维护集合”。

Example:

例子:

myCollection = Backbone.Collection.extend({
    comparator:function(){
        return item.get('name');
    }
});

IN GENERAL

在一般情况下

What you are expected to do when 'backboning' (developing using backbone.js framework) is to extend the given object (for instance a View) with:

当你使用骨干网时,你应该做什么?(js框架)将给定对象(例如视图)扩展为:

window.ButtonView = Backbone.View.extend({
    btnText:'nothingByDefault',
    myNewMethod:function(){
       //do whatever you want, maybe do something triggered by an event, for instance
    }
});

and then use it somewhere else in the code, once for each button you want to handle, including in the braces all the values you want to give to the object

然后在代码中的其他地方使用它,对每个要处理的按钮,包括在大括号中给对象的所有值

[...]
var submitBtn = new ButtonView({btnText:"SubmitMe!"}),
var cancelBtn = new ButtonView({btnText:"Erase All!"});

....Hope this helps...

....希望这有助于……

#2


14  

In the second case TestModel is a contructor which you can use several times later to create an instance of model:

在第二种情况下,TestModel是一个contructor,您可以在以后多次使用它来创建一个model的实例:

var model = new TestModel();

However passing title to extend has a different meaning. You should probably use:

然而,通过标题扩展有不同的含义。你应该使用:

var TestModel = Backbone.Model.extend({defaults: { title: "test title" }});

or pass the model attributes when creating an object:

或在创建对象时传递模型属性:

var model = new TestModel({ title: "test title" });

In the first case on the other hand TestModel is already an instance of model (hence it should be named testModel to follow JavaScript naming convention):

在另一种情况下,TestModel已经是模型的实例(因此它应该被命名为TestModel,以遵循JavaScript命名约定):

var testModel = new Backbone.Model({ title: "test title" })

#1


74  

There is a basic difference, which in short can be described as "the difference between the project of a house and the house itself".

有一个基本的区别,简而言之就是“房子的项目和房子本身的区别”。

For expert programmers I would just say that "new Backbone.Model" returns an object instance, but "Backbone.Model.extend" returns a constructor function

对于专业的程序员,我只想说“新主干”。Model“返回一个对象实例,但是”Backbone.Model”。“扩展”返回构造函数

FIRST: a new object (i.e. The house)

首先:一个新的物体(例如房子)

var TestModel = new Backbone.Model({ title: "test title" });

you create a new Object whose structure (methods and variables) have been defined somewhere else. Object can be considered as "all the non-native items" of a language, where for "native item" I mean basic types like integers, characters etc.

您将创建一个新对象,该对象的结构(方法和变量)已在其他地方定义。对象可以被认为是一种语言的“所有非本机项”,其中“本机项”指的是基本类型,如整数、字符等。

In the braces {} you pass the value of some variable or method. This is called, as Tomasz Nurkiewicz previously explained, a constructor, because it allows you to 'construct' a new object whose model has been described elsewhere.

在大括号{}中,传递某个变量或方法的值。正如Tomasz Nurkiewicz先前解释的那样,这被称为构造函数,因为它允许您“构建”一个新对象,该对象的模型已经在其他地方被描述过。

To give you a known example: you write

给你一个众所周知的例子:你写作

var myArray = new Array();

It means that you are creating a new Array, which is a non-native object which has been defined elsewhere. you can also write:

这意味着您正在创建一个新的数组,该数组是在其他地方定义的非本地对象。你也可以写:

var myArray = new Array([1,2,3,4,5]);

And it fills the array with the given numbers.

它用给定的数字填充数组。

SECOND: modify definition of an existing object (i.e. The project of the house)

第二:修改现有对象(即房屋项目)的定义

with

var TestModel = Backbone.Model.extend({ title: "test title" })

you say something very simple to your VM: "the object you give me as default is very nice, but I want to implement more functions/properties". So with the "extend" clause you modify the definition of an object adding or override existing method/properties.

你对你的VM说了一些非常简单的话:“你给我的默认对象非常好,但是我想实现更多的函数/属性”。因此,使用“extend”子句可以修改对象的定义,添加或覆盖现有方法/属性。

Example: a nice example in backbone.js is given by the comparator function of a collection. When you extend the object defining it, "it will be used to maintain the collection in sorted order".

示例:主干中的一个很好的示例。js是由一个集合的比较器函数给出的。当您扩展定义它的对象时,“它将用于以排序的顺序维护集合”。

Example:

例子:

myCollection = Backbone.Collection.extend({
    comparator:function(){
        return item.get('name');
    }
});

IN GENERAL

在一般情况下

What you are expected to do when 'backboning' (developing using backbone.js framework) is to extend the given object (for instance a View) with:

当你使用骨干网时,你应该做什么?(js框架)将给定对象(例如视图)扩展为:

window.ButtonView = Backbone.View.extend({
    btnText:'nothingByDefault',
    myNewMethod:function(){
       //do whatever you want, maybe do something triggered by an event, for instance
    }
});

and then use it somewhere else in the code, once for each button you want to handle, including in the braces all the values you want to give to the object

然后在代码中的其他地方使用它,对每个要处理的按钮,包括在大括号中给对象的所有值

[...]
var submitBtn = new ButtonView({btnText:"SubmitMe!"}),
var cancelBtn = new ButtonView({btnText:"Erase All!"});

....Hope this helps...

....希望这有助于……

#2


14  

In the second case TestModel is a contructor which you can use several times later to create an instance of model:

在第二种情况下,TestModel是一个contructor,您可以在以后多次使用它来创建一个model的实例:

var model = new TestModel();

However passing title to extend has a different meaning. You should probably use:

然而,通过标题扩展有不同的含义。你应该使用:

var TestModel = Backbone.Model.extend({defaults: { title: "test title" }});

or pass the model attributes when creating an object:

或在创建对象时传递模型属性:

var model = new TestModel({ title: "test title" });

In the first case on the other hand TestModel is already an instance of model (hence it should be named testModel to follow JavaScript naming convention):

在另一种情况下,TestModel已经是模型的实例(因此它应该被命名为TestModel,以遵循JavaScript命名约定):

var testModel = new Backbone.Model({ title: "test title" })