new Backbone.Model() 与 Backbone.Model.extend() [英] new Backbone.Model() vs Backbone.Model.extend()

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

问题描述

我是 JS 和 Backbone 的新手

I'm new to JS and Backbone

这两者有什么区别?

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

对于专业程序员,我只想说new Backbone.Model"返回一个对象实例,但Backbone.Model.extend"返回一个构造函数

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

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

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.

在大括号 {} 中传递一些变量或方法的值.正如 Tomasz Nurkiewicz 之前解释的那样,这被称为构造函数,因为它允许您构造"一个​​新对象,其模型已在别处进行了描述.

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

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.

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

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.

示例:backbone.js 中的一个很好的示例由集合的比较器函数给出.当您扩展定义它的对象时,它将用于按排序顺序维护集合".

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

示例:

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

一般情况

当骨干"(使用backboning.js 框架开发)扩展给定对象(例如视图)时,您应该做什么:

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

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

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