简体   繁体   English

扩展Ember.Object时为什么省略var语句?

[英]Why is the var statement omitted when extending Ember.Object?

From the Ember.js documentation : Ember.js文档中

Person = Ember.Object.extend({
   // [..]
});

It's the first definition of Person , so why is the var statement omitted? 这是Person的第一个定义,那么为什么省略var语句?


var is also missing in some other places, eg Ember.Application.Create or Ember.StateManager.Create . 在其他一些地方也缺少var ,例如Ember.Application.CreateEmber.StateManager.Create


Guys, I know what happens when var is omitted. 伙计们,我知道省略var时会发生什么。 But there doesn't seem to be a good reason to do it, like this it's just confusing people. 但是这样做似乎没有充分的理由,因为这样只会使人感到困惑。

It's declared globally so Ember and Handlebars can resolve bindings. 它是全局声明的,因此Ember和Handlebars可以解析绑定。

In case of a view for example it's necessary so it can be instantiated in a Handlebars template via the view helper: 例如,在视图的情况下,有必要使它可以通过view助手在Handlebars模板中实例化:

Handlebars : 车把

<script type="text/x-handlebars" >
    {{#view MyView}}
         my view's template
    {{/view}}
</script>

JavaScript :​ JavaScript

MyView = Ember.View.extend({});

The following example doesn't work when the controller is declared with a var statement, see http://jsfiddle.net/pangratz666/uzsd6/ : 当使用var语句声明控制器时,以下示例不起作用,请参见http://jsfiddle.net/pangratz666/uzsd6/

Handlebars : 车把

<script type="text/x-handlebars" >
    {{controller.name}} - {{secondController.name}}
</script>​

JavaScript : JavaScript

var controller = Ember.Object.create({
    name: 'my name'
});

var secondController = Ember.Object.create({
    nameBinding: 'controller.name'
});​

If the controllers are declared on a global available object, the App namespace in this case, the the bindings can be resolved, see http://jsfiddle.net/pangratz666/kUmje/ : 如果在全局可用对象(在这种情况下为App命名空间)上声明了控制器,则可以解析绑定,请参见http://jsfiddle.net/pangratz666/kUmje/

Handlebars : 车把

<script type="text/x-handlebars" >
    {{App.controller.name}} - {{App.secondController.name}}
</script>​

JavaScript : JavaScript

App = Ember.Application.create({});

App.controller = Ember.Object.create({
    name: 'my name'
});

App.secondController = Ember.Object.create({
    nameBinding: 'App.controller.name'
});​

You should take a look at the Emberist 's blog about Naming Conventions in Ember.js . 您应该看一下EmberistEmber.js有关命名约定的博客。

You only use the var keyword if you want to declare the variable in the local scope. 如果要在本地作用域中声明变量,则仅使用var关键字。 Declaring a variable without var will result in the variable being available in the global scope. 声明不带var的变量将导致该变量在全局范围内可用。 This is usually done with classes as you want to access them from anywhere in most cases. 通常,这是通过类完成的,因为在大多数情况下,您想从任何地方访问它们。

This statement, if not preceded by var Person always creates variable in global scope (window in browser's case), so I guess it's just sloppiness or there may be some reason Person should be global and accessible from anywhere. 该语句,如果未在var Person之前加上,则始终会在全局范围内创建变量(在浏览器的情况下为窗口),因此我认为这只是草率,或者可能由于某些原因,Person应该是全局的并且可以从任何地方访问。 I agree with @Femaref, this is a pseudo "Class" so it is meant to construct instances like var person = new Person() therefore it should be accesible from other places. 我同意@Femaref,这是一个伪“类”,因此它的意思是构造诸如var person = new Person()类的实例,因此应该可以从其他地方访问它。

Anyway, it's always a bad habit to pollute the global namespace. 无论如何,污染全局名称空间总是一个坏习惯。 The person class should reside in some "namespace hash". 人员类应驻留在某些“命名空间哈希”中。

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

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