Is there a way to find out which class belongs to a function? Example:
function globalFunc(){
Now im uses this workaround:
function globalFunc(){ alert(globalFunc.caller.__class__); } function MyObject(){ } MyObject.prototype.test=function(){ globalFunc(); } MyObject.prototype.test.__class__=MyObject; var o=new MyObject(); o.test();
But there is a big problem, look:
function globalFunc(){ alert(globalFunc.caller.__class__); } function MyObject(){ } MyObject.prototype.test=function(){ var temp=function(){ globalFunc(); } temp(); } MyObject.prototype.test.__class__=MyObject; var o=new MyObject(); o.test();
So, is there a clear way to get this? I know where the problem is (the class property is only a property of the test, not temp), but I cannot add class to temp either.
Thanks.
Thanks for the answer, some clarifications.
I am trying to make a personal OO framework focused on private members.
So:
globalFunc is a special function, im using it to get the "private" property, and I can not call it using the method of calling or passing some arguments, the only arguments to im pass are "this":
Example: $ () is global
Class({ public:{ MyClass:function(){ }, setName:function(name) { $(this).name=name; //set the private var name }, getName:function(){ return $(this).name; } }, private:{ name:"UNKNOWN" } }) var o=new MyClass(); o.getName(); // UNKNOWN o.setName("TEST!!!"); o.getName(); // TEST!!! o.name; //undefined $(o).name; //undefined
To work with inheritance, $ (), I need to know which class calls it and the class object.
Everything works well, but if I need to access private members in clousure, I have to add the __ class __ property of clouser !! And I do not want this!
Thanks again and sorry for my poor English, I'm not a native speaker.
javascript object methods class
user189086
source share