使用下划线与主干延长() [英] extend() using underscore vs. backbone

查看:140
本文介绍了使用下划线与主干延长()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道骨干在一定程度取决于下划线和jQuery。是否有下面的两条线之间的区别?

I know backbone is somewhat depending on underscore and jquery. Is there difference between the two lines below?

app.notifications = _.extend({}, Backbone.Events);

app.notifications  = Backbone.Events.extend({});

如果他们的不可以一样,有什么不同?

If they are NOT the same, how different?

推荐答案

Backbone.Events.extend不存在,
所以我将把Backbone.Model来代替。

Backbone.Events.extend does not exist, so I will refer to Backbone.Model instead.

_。扩展(目标,mixin1,mixin2)是要属性复制到目标对象

_.extend(target, mixin1, mixin2) is going to copy properties into the target object

Backbone.Model.extend将会子类 Backbone.Model基本上使一个构造函数(功能),其原型有您所提供的性能。这将允许你做出新的类的实例

Backbone.Model.extend is going to subclass Backbone.Model basically make a constructor (function) whose prototype has your provided properties. This will allow you to make instances of your new class

var Person = Backbone.Model.extend({name: 'yourName'});
var me = new Person();
alert(me.name);

,而 _。延长将失败

var Person = _.extend({name: 'yourName'}, Backbone.Model);
var me = new Person();  //error b/c Person is a regular object
alert(me.name);

在短期Backbone.Model.extend创建一个新构造(功能),而_.extend修改现有对象;

In short Backbone.Model.extend creates a new constructor (function), while _.extend modifies an existing object;

var modified = {};
alert(modified === _.extend(modified, Backbone.Model)); //true
alert(modified === Backbone.Model.extend(modified)); //false

这篇关于使用下划线与主干延长()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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