Javascript - get the class of the owner of a function (method)

Is there a way to find out which class belongs to a function? Example:

function globalFunc(){ //alert MyObject } function MyObject(){ } MyObject.prototype.test=function(){ globalFunc(); } var o=new MyObject(); o.test(); //alert MyObject 

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(); //alert MyObject 

But there is a big problem, look:

 function globalFunc(){ alert(globalFunc.caller.__class__); } function MyObject(){ } MyObject.prototype.test=function(){ var temp=function(){ globalFunc(); } temp(); /* to simulate a simple closure, this may be for example: element.addEventListener("click",temp,false); */ } MyObject.prototype.test.__class__=MyObject; var o=new MyObject(); o.test(); //alert undefined 

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.

+6
javascript object methods class
source share
7 answers

There are no classes in javascript. Instead, multiple objects may own the same function. For example:

 function myFun(){ alert(this.name); } function Obj1(){ this.name = "obj1"; } Obj1.prototype.fun = myFun; function Obj2(){ this.name = "obj2"; } Obj2.prototype.fun = myFun; var obj1 = new Obj1(); var obj2 = new Obj2(); obj1.fun(); obj2.fun(); 
+3
source share

The scenario is not entirely clear, but here are a few options: -

  function globalFunc() { alert(this.__class__); //Note globalFunc now has access to much more. } function MyObject(){ } MyObject.prototype.test=function(){ globalFunc.call(this); } MyObject.prototype.__class__=MyObject; 

To add closure for event handling

 MyObject.prototype.test = function(){ var self = this; var elem = //get some element; //Not cross-browser but for illustration elem.addEventListener('click', fnEvent); function fnEvent() { globalFunc.call(self); } elem = null } 
+2
source share

I don’t understand what you are trying to do, but here is an idea that can inspire you.
The constructor property helped me well when I tried to use JS as an OO language.

o.constructor will provide you with a link to myObject .

But, in my opinion, you should give functional programming instead of OO in order to make the most of Javascript

+1
source share

temp is caller, but it does not have the __class__ property. You just determined that for test .

0
source share

caller non-standard and really fragile; best avoided. It is much better to pass the value explicitly:

 MyObject.prototype.test= function() { element.addEventListener('click', function() { globalFunc(MyObject); }); }; 

or

 MyObject.prototype.test= function() { var myclass= arguments.callee.__class__; element.addEventListener('click', function() { globalFunc(myclass); }); }; 
0
source share

Functions are not "owned" in JavaScript. Functions are first-class objects in JavaScript, which means that they can be passed like any other variable and assigned as properties of many objects.

this inside the body of the function will give you a reference to the object that the function was called if called as a method (for example, myObj.myMethod() ) or using the prototype methods of the call() or apply() function (for example, myFunc.call(myObj) ). Inside a function that is called by itself (for example, myFunc() ), this will usually be a global object (just like window in most browsers).

0
source share

Short answer, there really is no way to do what you are trying to do. Javascript just doesn't work.

Long answer ...

Almost every programmer I met who came to JavaScript from a language using the classical object model tried to do what you do: emulate the classic OOP style that they use with Javascript. There is no shame here, I did it, even the famous Douglas Crockford experimented with it, and then later refused. So do I. I still think it was necessary for me to try this material in order to really learn langauage.

Do not be fooled by curly braces and familiar operators. This kernel has very little to do with Java! JavaScript is a functional langauge: functions are objects in their own right.

Straight: there is no way to do what you are trying to do is to have objects with private properties that you can access through

Saying it is easy :) Understanding how to apply it is more difficult

There are no classes, there are only objects. Objects have only properties. The properties of objects may matter. These values ​​may be functions. this is set when the function is called, it can be anything

Closing is the only way to achieve true privacy in JavaScript. Everything else either flows in some encompassing area, relies on obfuscated property names, or can be somehow bypassed by the caller, or a memory leak even after your objects are no longer referenced (since there is no destructor function to clean in).

What you do is still good, what you are trying to do, trying to do it, you will become a better JavaScript programmer by figuring out why you cannot do this more importantly, why you probably shouldn't

0
source share

All Articles