[Moo] Class Name

4 messages Options
Embed this post
Permalink
samdev

[Moo] Class Name

Reply Threaded More More options
Print post
Permalink

I want to throw an Exception and tell user what caused exception in a
form:

  Class::Method

Method name can easily be obtained from:

  arguments.callee

and then processing returned string.

How to get class name inside method?

I create classes next way:

var MyClass = new Class({
  ...
  method: function()
  {
    console.log( 'Want to output class name here.');
  },
  ...
});

Jan Kassens

[Moo] Re: Class Name

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)
You can embed the name of the class in the class itself, like:

var MyClass = new Class({
name: ‘MyClass’,
method: function(){
console.log(this.name);
}
});


or iterate over all variables in window. This doesn’t work with classes that are not in the window space (like Fx.Tween for instance) and is not the fastest solution, but might be enough for your case:

Class.getClassName = function(obj){
for (name in window){
if (window[name] == obj.constructor) return name;
}
return null;
};

var MyClass = new Class({
method: function(){
console.log(Class.getClassName(this));
}
});


Jan


On 05.11.2009, at 15:52, Sam wrote:


I want to throw an Exception and tell user what caused exception in a
form:

 Class::Method

Method name can easily be obtained from:

 arguments.callee

and then processing returned string.

How to get class name inside method?

I create classes next way:

var MyClass = new Class({
 ...
 method: function()
 {
   console.log( 'Want to output class name here.');
 },
 ...
});


anutron

[Moo] Re: Class Name

Reply Threaded More More options
Print post
Permalink
STOP YELLING AT ME!

...kidding.

On Thu, Nov 5, 2009 at 10:38 AM, Jan Kassens <[hidden email]> wrote:
You can embed the name of the class in the class itself, like:

var MyClass = new Class({
name: ‘MyClass’,
method: function(){
console.log(this.name);
}
});


or iterate over all variables in window. This doesn’t work with classes that are not in the window space (like Fx.Tween for instance) and is not the fastest solution, but might be enough for your case:

Class.getClassName = function(obj){
for (name in window){
if (window[name] == obj.constructor) return name;
}
return null;
};

var MyClass = new Class({
method: function(){
console.log(Class.getClassName(this));
}
});


Jan


On 05.11.2009, at 15:52, Sam wrote:


I want to throw an Exception and tell user what caused exception in a
form:

 Class::Method

Method name can easily be obtained from:

 arguments.callee

and then processing returned string.

How to get class name inside method?

I create classes next way:

var MyClass = new Class({
 ...
 method: function()
 {
   console.log( 'Want to output class name here.');
 },
 ...
});



The MooTools Tutorial: www.mootorial.com Clientcide: www.clientcide.com
Jan Kassens

[Moo] Re: Class Name

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)
Huh, I wonder why it sent in bold font.

Sorry ‘bout that.

Jan

On 05.11.2009, at 20:04, Aaron Newton wrote:

STOP YELLING AT ME!

...kidding.

On Thu, Nov 5, 2009 at 10:38 AM, Jan Kassens <[hidden email]> wrote:
You can embed the name of the class in the class itself, like:

var MyClass = new Class({
name: ‘MyClass’,
method: function(){
console.log(this.name);
}
});


or iterate over all variables in window. This doesn’t work with classes that are not in the window space (like Fx.Tween for instance) and is not the fastest solution, but might be enough for your case:

Class.getClassName = function(obj){
for (name in window){
if (window[name] == obj.constructor) return name;
}
return null;
};

var MyClass = new Class({
method: function(){
console.log(Class.getClassName(this));
}
});


Jan


On 05.11.2009, at 15:52, Sam wrote:


I want to throw an Exception and tell user what caused exception in a
form:

 Class::Method

Method name can easily be obtained from:

 arguments.callee

and then processing returned string.

How to get class name inside method?

I create classes next way:

var MyClass = new Class({
 ...
 method: function()
 {
   console.log( 'Want to output class name here.');
 },
 ...
});