Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  114] [ 5]  / answers: 1 / hits: 51501  / 13 Years ago, sun, may 22, 2011, 12:00:00

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


More From » javascript

 Answers
29

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

[#92125] Thursday, May 19, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarettajb

Total Points: 678
Total Questions: 94
Total Answers: 90

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;