简体   繁体   English

这两种JavaScript继承方式有什么区别

[英]What's the difference between these two inheritance ways in JavaScript

Today I wasted hours in a weird bug while developing ember 今天,我在开发余烬时浪费了数小时的怪异错误

I confidently ignored the Ember extend way 我自信地忽略了Ember的延伸方式

App.SomeModel = DS.Model.extend()

and somehow turned into my prefer way 并以某种方式变成了我的首选方式

class App.SomeModel extends DS.Model()

My second way just didn't work. 我的第二种方法不起作用。 So I want to know what's the difference between these two and why the second way didn't work in Ember(Cuz' both ways work in Backbone) 所以我想知道这两者之间有什么区别,以及为什么第二种方法在Ember中不起作用(因为Cuz的两种方法都在Backbone中起作用)

Look at EmberScript 看看EmberScript

http://emberscript.com/ http://emberscript.com/

The key difference is that the Class and extends compile directly to the Ember equivalents, rather than trying to make the Coffeescript ideas fit with Ember. 主要区别在于Classextends直接编译为Ember等效项,而不是尝试使Coffeescript想法适合Ember。

class SomeModel extends Ember.Object

becomes 变成

var SomeModel;
var get$ = Ember.get;
var set$ = Ember.set;
SomeModel = Ember.Object.extend();
App.SomeModel = DS.Model.extend()

This calls Ember.js's own Object extend method, which adds observers, reopens a class and so on. 这将调用Ember.js自己的Object扩展方法,该方法添加观察者,重新打开类,等等。

class App.SomeModel extends DS.Model()

Doesn't rely on a framework, In plain javascript, It's assigning "Somemodel" the properties of the "DS.Model()" object. 不依赖于框架,在简单的javascript中,它为“ Somemodel”分配了“ DS.Model()”对象的属性。 It's not expected to work inside the framework for extending Ember.Object's 不应在扩展Ember.Object的框架内工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM