使用 CoffeeScript `extends` 与 Backbone.js `extend` 的根本区别 [英] Fundamental differences between utilizing CoffeeScript `extends` vs. Backbone.js `extend`
本文介绍了使用 CoffeeScript `extends` 与 Backbone.js `extend` 的根本区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 CoffeeScript extends
与 Backbone.js extend
的根本区别是什么?
What are the fundamental differences between utilizing CoffeeScript extends
vs. Backbone.js extend
?
例如,怎么样
class User extends Backbone.Model
不同于
User = Backbone.Model.extend()
推荐答案
两者是等价的.引用 Backbone.js 变更日志:
The two are intended to be equivalent. To quote the Backbone.js changelog:
0.3.0:Backbone 类现在可以被 CoffeeScript 类无缝继承.
0.3.0: Backbone classes may now be seamlessly inherited by CoffeeScript classes.
CoffeeScript 的 Child extends Parent
和 Backbone 的 Child = Parent.extend()
都做了三个重要的事情:
Both CoffeeScript's Child extends Parent
and Backbone's Child = Parent.extend()
do three important things:
- (最重要的) 他们将
Child.prototype
设置为new ctor
,其中ctor
是一个函数,其原型是Parent.prototype
.这建立了原型继承. - 他们将
Parent
的所有静态属性复制到Child
. - 他们设置
Child.__super__ = Parent
.这主要是为了在Child
的方法中支持CoffeeScript的类似Ruby的super
关键字.
- (Most important) They set
Child.prototype
tonew ctor
, wherector
is a function whose prototype isParent.prototype
. That establishes prototypal inheritance. - They copy all of
Parent
's static properties ontoChild
. - They set
Child.__super__ = Parent
. This is mainly to support CoffeeScript's Ruby-likesuper
keyword inChild
's methods.
这篇关于使用 CoffeeScript `extends` 与 Backbone.js `extend` 的根本区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文