goog.scope() is not meant to alias defining class itself, right?

129 views
Skip to first unread message

Alexander Samilyak

unread,
May 22, 2012, 1:02:49 PM5/22/12
to closure-lib...@googlegroups.com
If I want to alias defining class using goog.scope, I should write like this:

=================================================
goog.provide('app.MyClass');


/**
 * @constructor
 */
app.MyClass = function() {
  /**
   * @type {!app.SomeType}
   * @private
   */
  this.f_ = new app.SomeType();
};


goog.scope(function() {
  var MyClass = app.MyClass;

  MyClass.prototype.myMethod = function() {};
});
=================================================


I should put constructor outside goog.scope, because I couldn't alias it for prototype methods otherwise.
Putting constructor outside making me write fully qualified types in constructor and that's killing the whole idea.

Title question - is goog.scope() meant to alias defining class itself?
Or it's for dependencies only and I should write
long.path.MyClass.prototype.<myMethod> = ....
many times anyway?

John Lenz

unread,
May 22, 2012, 8:42:42 PM5/22/12
to closure-lib...@googlegroups.com
You can't alias a property before it is assigned:

var MyClass = app.MyClass;
MyClass = function() { ...}  // This would not assign to the property "app.MyClass"

but you can alias the namespace it is contained in:

var ns = my.app.namespace;
ns.MyClass = function() {};

Alexander Samilyak

unread,
May 23, 2012, 6:06:19 AM5/23/12
to closure-lib...@googlegroups.com
You can't alias a property before it is assigned:

var MyClass = app.MyClass;
MyClass = function() { ...}  // This would not assign to the property "app.MyClass"

You're absolutely right and that's what was making me to ask my question. My example (constructor outside goog.scope) is really inconvenient.
And I'm not comfortable with the idea to alias namespace of defining class - you can't use alias with the name different from original namespace and I was hoping to find something more elegant than "lastPackage.MyClass".

So I'll reword my question:
does google offer some style for aliasing current class using goog.scope
(except aliasing last namespace)
?
Reply all
Reply to author
Forward
0 new messages