使用 CoffeeScript `extends` 与 Backbone.js `extend` 的根本区别 [英] Fundamental differences between utilizing CoffeeScript `extends` vs. Backbone.js `extend`

查看:11
本文介绍了使用 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:

  1. (最重要的) 他们将 Child.prototype 设置为 new ctor,其中 ctor 是一个函数,其原型是 Parent.prototype.这建立了原型继承.
  2. 他们将 Parent 的所有静态属性复制到 Child.
  3. 他们设置 Child.__super__ = Parent.这主要是为了在Child的方法中支持CoffeeScript的类似Ruby的super关键字.
  1. (Most important) They set Child.prototype to new ctor, where ctor is a function whose prototype is Parent.prototype. That establishes prototypal inheritance.
  2. They copy all of Parent's static properties onto Child.
  3. They set Child.__super__ = Parent. This is mainly to support CoffeeScript's Ruby-like super keyword in Child's methods.

这篇关于使用 CoffeeScript `extends` 与 Backbone.js `extend` 的根本区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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