简体   繁体   中英

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.

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";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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