Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refer to javascript function from within itself

Tags:

javascript

Consider this piece of code

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

From inside crazy() 'this' refers to the window, which I guess makes sense because normally you'd want this to refer to the object the function is attached to, but how can I get the function to refer to itself, and access a property set on itself?

Answer:

Don't use arguments.callee, just use a named function.

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

like image 391
timoxley Avatar asked May 22 '11 04:05

timoxley


People also ask

How do you call a function from itself in JavaScript?

A function that calls itself is called a recursive function. The syntax for recursive function is: function recurse() { // function code recurse(); // function code } recurse();

How do you call a function within itself?

Recursion is a programming term that means calling a function from itself. Recursive functions can be used to solve tasks in elegant ways. When a function calls itself, that's called a recursion step.

Can you call the same function within a function JavaScript?

Calling a function from within itself is called recursion and the simple answer is, yes.

How do you define a function in JavaScript?

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). (parameter1, parameter2, ...)

Can you find a JavaScript function's name from inside of itself?

It's rare, but occasionally it's helpful to discover a JavaScript function's name from inside of itself. Even with years of JavaScript experience, I find myself having to hunt this technique down, and then the examples are never precisely what I'm after, so they need to be tweaked...it's all a big time suck.

What is the purpose of a self invoked function in JavaScript?

This Self Invoking function are mainly for variable scoping. By default, variables declared in self invoked functions are only available to code within the self-invoked function. Does not care about how variables are declared or named in another block of code in JavaScript.

What does it mean when JavaScript returns a value?

If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement. Functions often compute a return value.


2 Answers

I think you are asking for arguments.callee, but it's deprecated now.

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 
like image 165
4 revs Avatar answered Sep 21 '22 13:09

4 revs


As rfw said, this is the most straight forward way to go if the function has one single name:

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

In case it may have different names, or you wanted to pass it around, it must be wrapped in a closure:

var crazy = (function(){     var that = function() {         console.log(that);         console.log(that.isCrazy);     };     return that; })();  crazy.isCrazy = 'totally'; crazy(); 
like image 28
zupa Avatar answered Sep 21 '22 13:09

zupa