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