Jared
Jared

Reputation: 3016

Which inherited class called function?

I want to find out the name of the class of an object from within a function that called that function. I am using John Resig's class inheritance concept.

For example

var CoreStuff = Class.extend({ log: function(Message) { console.log(who + ' said ' + Message); });

var MyApp = CoreStuff.extend({ init: function() { this.log('Hello world!'); });

var app = new MyApp();

How do I find out if MyApp or CoreStuff or any other object called log() within the hierarchy?

I don't want to pass any more parameters with the current class as I do that currently.

Upvotes: 2

Views: 117

Answers (2)

Gad
Gad

Reputation: 42316

This may seem old, but in case anyone still wonders I encountered the issue recently. The reason why everything is called Class is because of the way that anonymous functions are used in John's script.

I've written an article about it here: http://www.nicolasbize.com/blog/javascript-inheritance-without-loosing-class-names/

And the proof of concept as a small js framework on github: http://www.nicolasbize.com/moojs

Upvotes: 0

Khanh TO
Khanh TO

Reputation: 48982

var CoreStuff = Class.extend({ log: function(Message) { 
                                         console.log(this.constructor.className + ' said ' + Message);                        }
                                    });
CoreStuff.className = "Core Stuff";    

var MyApp = CoreStuff.extend({ init: function() { this.log('Hello world!'); }});
MyApp.className = "My App";

Upvotes: 2

Related Questions