Access javascript function from inside yourself

Consider this piece of code

var crazy = function() { console.log(this); console.log(this.isCrazy); // wrong. } crazy.isCrazy = 'totally'; crazy(); // ouput => // DOMWindow // undefined 

From the inside crazy () 'this' refers to a window, which I think makes sense, because usually you need it to refer to the object to which the function is attached, but how can I make the function refer to itself and access a set of properties on your own?

Answer:

Do not use arguments.callee, just use a named function.

"Note. You should avoid using arguments.callee () and just give each function (expression) a name." via MDN article on arguments.callee

+47
javascript
May 22 '11 at 4:20
source share
10 answers

I think you are asking for arguments. callee, but now it's out of date.

https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments/callee

 var crazy = function() { console.log(this); console.log(arguments.callee.isCrazy); // right. } crazy.isCrazy = 'totally'; crazy(); // ouput => // DOMWindow // totally 
+26
May 22, '11 at 4:40
source share

As rfw said, this is the most direct way if the function has one name:

 var crazy = function() { console.log(crazy); console.log(crazy.isCrazy); }; crazy.isCrazy = 'totally'; crazy(); 

If it can have different names or you want to transfer them, it should be wrapped in closure:

 var crazy = (function(){ var that = function() { console.log(that); console.log(that.isCrazy); }; return that; })(); crazy.isCrazy = 'totally'; crazy(); 
+13
Jul 10 '12 at 18:30
source share

You must give it your name, therefore:

 var crazy = function() { console.log(crazy); console.log(crazy.isCrazy); } crazy.isCrazy = 'totally'; crazy(); 

This variable is applicable only in the area of ​​the object, for example, if you called your version of the crazy function using crazy.call(crazy) , it is called by the function in the context of the crazy function and everything will be fine.

+4
May 22 '11 at 4:23
source share
+2
May 22 '11 at 4:33 a.m.
source share

You can use the call method

 var crazy = function() { console.log(this); console.log(this.isCrazy); } crazy.isCrazy = 'totally'; crazy.call(crazy); // calls crazy using crazy as the target, instead of window: // functionToCall.call(objectToUseForThis); 

Although if your function has only one name, you can do this:

 var crazy = function() { console.log(crazy); console.log(crazy.isCrazy); } crazy.isCrazy = 'totally'; crazy(); 
+2
May 22 '11 at 4:33
source share

How can I make a function refer to itself?

The idea of ​​"self" does not exist with functions. You need an object, not just a function. An object has knowledge of itself through the keyword 'this'. Inside the function 'this' points to a global object - in this case, a window object. But if you use the function as a constructor function to create an object (using the new operator), then the pointer to the 'this' object will point to the object itself.

ie this points to an object if you write:

 var anObject = new crazy(); 

So, you can rewrite your code as follows:

 var crazy = function() { this.printMe = function(){ console.log(this); console.log(this.isCrazy); } } var anObject = new crazy(); //create an object anObject.isCrazy = 'totally'; //add a new property to the object anObject.printMe(); //now print 

If you want to add a property before creating the object, you need to add the property to the function prototype as follows:

 var crazy = function() { console.log(this); console.log(this.isCrazy); } crazy.prototype.isCrazy = 'totally'; //add the property to the function prototype var anObject = new crazy(); //invoke the constructor 

Learn more about my blog for a detailed explanation of these concepts using code samples.

+2
May 22 '11 at 10:55
source share

Bind the function to yourself (taking a hint from the answers of @ArunPJohny and @BudgieInWA):

 crazy = crazy.bind(crazy); 

This will give you access from the function to its properties through this .

 > crazy() function () { console.log(this); console.log(this.isCrazy); // works now } 

This seems like a better solution than the accepted answer, which uses the callee function, which is deprecated and does not work in strict mode.

You can also now call a function call recursively with this() if you were so prone.

We will call it self-determining . Write a small useful feature:

 function selfthisify(fn) { return fn.bind(fn); } crazy = selfthisify(crazy); crazy(); 

Or, if you prefer more “semantic" names, you can call it accessOwnProps .

If you are a syntactic type of sugar type, you can add the selfthisify property to the function prototype:

 Object.defineProperty(Function.prototype, 'selfthisify', { get: function() { return this.bind(this); } }); 

Now you can say

 crazy.selfthisify(); 
+2
Oct 16 '14 at 13:16
source share

The easiest way to make a function the most accessible in your body is to make var crazy = function crazy2() { crazy2(); } var crazy = function crazy2() { crazy2(); } , this is normal for crazy and crazy2 with the same name, since the first occurrence is the name in the outer scope, and the second is the name in the function body.

Or just function crazy() { crazy(); } function crazy() { crazy(); } , which will determine the craziness in both areas.

+1
May 22 '11 at 7:10
source share

Are you really trying to create a class object?

 function crazy(crazyState) { this.isCrazy = crazyState; console.log(this); console.log(this.isCrazy); } crazy.prototype.alertMe = function() { alert('I am '+ this.isCrazy +' crazy.'); } var crazyObj = new crazy('totally'); crazyObj.alertMe(); crazyObj.isCrazy = 'not'; crazyObj.alertMe(); 
0
May 22 '11 at 4:32 a.m.
source share

It's funny what you have to ask, pal. I just went through the same problem for another purpose . Quick version of the final code:

 $a = function() {}; $ = function() { if (!(this instanceof $)) { return new $(); } this.name = "levi"; return this; }; //helper function var log = function(message) { document.write((message ? message : '') + "<br/>"); }; log("$().name == window.name: " + ($().name == window.name)); //false log("$().name: " + $().name); //levi log("window.name: " + window.name); //result log(); log("$a instanceof $: " + ($a instanceof $)); //false log("typeof $a: " + (typeof $a)); //function log("typeof $: " + (typeof $)); //function 

The critical part:

  if (!(this instanceof $)) { return new $(); } 

If this does not point to an object of the correct type, then it makes new one that this will properly possess. The rest of the code is there to verify that it really works as intended.

0
May 22 '11 at 6:29 a.m.
source share



All Articles