简体   繁体   English

Ember.js中的类和对象混淆

[英]Class and Object confusion within Ember.js

This is a very basic question but I'm a little bit confounded. 这是一个非常基本的问题,但我有些困惑。 My understanding is that convention dictates that class definitions are capitalised but object instances are not. 我的理解是,约定规定类定义使用大写字母,而对象实例不使用大写字母。 So when I do the following: 因此,当我执行以下操作时:

App = require("app");
module.exports = App.AuthManager = Ember.Object.extend({
  apiKey: null,

  // Load the current user if the cookies exist and is valid
  init: function() {
    this._super();
    var accessToken = $.cookie('access_token');
    var authUserId  = $.cookie('auth_user');
    if (!Ember.isEmpty(accessToken) && !Ember.isEmpty(authUserId)) {
        this.authenticate(accessToken, authUserId);
    }
  },

  // ... 
}

I assume I am only defining the AuthManager's class definition. 我假设我只是在定义AuthManager的类定义。 Then if I do this: 然后,如果我这样做:

module.exports = App.ApplicationRoute = Ember.Route.extend({
    init: function() {
        this._super(); 
        App.AuthManager = App.AuthManager.create(); 
    }
});

My understanding is that App.AuthManager = App.AuthManager.create(); 我的理解是App.AuthManager = App.AuthManager.create(); instantiates the instance and that the ApplicationRouter is pretty much the first thing that is executed in an ember app. 实例化实例,并且ApplicationRouter几乎是在ember应用程序中执行的第一件事。 Is that right? 那正确吗? If so, shouldn't convention dictate that the instance be called authManager ? 如果是这样,约定不应该规定该实例称为authManager吗? Also, is it typical to put the class definition into the same namespace as the object? 另外,将类定义放入与对象相同的名称空间中是否很典型? I suspect this may come down to my relatively shallow understanding of JS but any help would be appreciated. 我怀疑这可能归结于我对JS的相对浅薄的理解,但是任何帮助都将不胜感激。

If so, shouldn't convention dictate that the instance be called authManager? 如果是这样,约定是否应规定该实例称为authManager?

Yes indeed, this line: 是的,的确如此:

 App.AuthManager = App.AuthManager.create();

should read, mostly to avoid confusion (although it's a singleton and singletons could also be capitalized) 应该阅读,主要是为了避免混淆(尽管这是一个单例,也可以将单例大写)

 App.authManager = App.AuthManager.create(); 

since your are creating an instance of the App.AuthManager class definition, this is at least how ember dictates it. 由于您正在创建App.AuthManager类定义的实例,因此至少这是ember指示的方式。

Also, is it typical to put the class definition into the same namespace as the object 另外,通常将类定义放入与对象相同的名称空间中

I guess not, (however Javascript let's you do all sort of things) therefore the lowercase naming of an Object instance, rather then a Capitalized like a class definition. 我猜不是, (但是Javascript让您做各种各样的事情)因此是Object实例的小写命名,而不是像类定义一样的大写。 So later on you could create more instances of the same class like: 因此,稍后您可以创建同一类的更多实例,例如:

App.foo = App.AuthManager.create();
App.bar = App.AuthManager.create();

(obviously this doesn't make much sense for a class that is meant to be a singleton, but you get the point) (显然,对于单身的班级来说,这没有多大意义,但您明白了)

Hope it helps 希望能帮助到你

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

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