新Backbone.Model()VS Backbone.Model.extend() [英] new Backbone.Model() vs Backbone.Model.extend()

查看:266
本文介绍了新Backbone.Model()VS Backbone.Model.extend()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的JS和骨干

什么是这两者之间有什么区别?

  TestModel =新Backbone.Model({标题:测试题})
TestModel = Backbone.Model.extend({标题:测试题})


解决方案

有一个基本的区别,这在短期可谓是一所房子的项目和房子本身的差别。

有关专家程序员,我只想说的新Backbone.Model返回一个对象实例,但Backbone.Model.extend返回一个构造函数

第一:一个新的对象
(即房子)

  TestModel =新Backbone.Model({标题:测试题});

您创建一个新的对象,其结构(方法和变量)已经确定在其他地方。对象可以被看作是一种语言,其中本地项目我的意思是基本类型,如整数,字符的所有的非本地项目等。

在括号{}你传递一些变量或方法的值。这就是所谓的,因为托马斯Nurkiewicz previously解释,构造,因为它可以让你结构的新对象已在别处描述的模型。

要给你一个已知的例子:你写

  myArray的=新的Array();

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

  myArray的=新的Array([1,2,3,4,5]);

和它填补了给定的数字数组。

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

  TestModel = Backbone.Model.extend({标题:测试题})

你说你的虚拟机的东西很简单:你给我默认的对象是非常好的,但我想实现更多的功能/性能。
所以,用延伸条款修改的对象添加的定义或重写现有的方法/属性。

例子:在一个很好的例子 Backbone.js的是一个集合的比较函数给出。当你扩展对象定义它,它会被用来维持有序集合。

例如:

  MyCollection的= Backbone.Collection.extend({
    比较:功能(){
       返回item.get('名');
       }
    });

一般

您预期在'backboning'做(= Backbone.js的使用框架来开发)的延长给定的对象(例如视图)与什么:

  window.ButtonView = Backbone.View.extend({
    btnText:nothingByDefault',
    myNewMethod:功能(){
       //做你想做的,也许做BBY触发一个事件东西,比如
       }
});

然后在code用在别处,一旦要处理,包括在大括号你想给对象的值的每个按钮

  [...]
VAR submitBtn =新ButtonView({btnText:SubmitMe})
VAR cancelBtn =新ButtonView({btnText:删除所有!});

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

I'm new to JS and Backbone

What's the difference between these two?

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

解决方案

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

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

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.

To give you a known example: you write

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:

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

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.

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".

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:

window.ButtonView = Backbone.View.extend({
    btnText:'nothingByDefault',
    myNewMethod:function(){
       //do whatever you want, maybe do something triggered bby 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...

这篇关于新Backbone.Model()VS Backbone.Model.extend()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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