Literal strings, numbers and booleans are primitives. That means they are not objects stricto sensu. But they derived from their correspondent native wrappers: String, Number and Boolean, which are objects, with native properties and methods. Therefor, they inherit those properties/methods through their wrapper's prototype. To be more precise it is a delegation, not an inheritance.
But if the wrapper is invoked as a constructor (ex: var mystring= new String()), the variable becomes the reference of a plain object, thus it can gain later individual members, same as any other Object.
Despite primitives, functions are objects. But their content is not a member of that object. Their content is a piece of code, a sub-routine which has to be run whenever the function is called.
Despite primitives, functions are objects. But their content is not a member of that object. Their content is a piece of code, a sub-routine which has to be run whenever the function is called.
So, given the above, is there a way for a function to refer to itself outside of a constructor without necessarily knowing its own name?
Yes, I have already mentioned that:
Code:
function fnc(){
var thisFunction=arguments.callee;
alert(thisFunction)
}
fnc();
There is a small confusion about the fact that arguments.callee is or not deprecated. Well, Mozilla stipulates clearly, I think:
"JavaScript 1.4: deprecated arguments, arguments.callee, and arguments.length as properties of Function instances; retained arguments as a local variable of a function and arguments.callee and arguments.length as properties of this variable.
There is a small confusion about the fact that arguments.callee is or not deprecated. Well, Mozilla stipulates clearly, I think:
"JavaScript 1.4: deprecated arguments, arguments.callee, and arguments.length as properties of Function instances; retained arguments as a local variable of a function and arguments.callee and arguments.length as properties of this variable.
It's a good idea to name all your functions anyways, so this isn't that big of a deal.
but i will miss argument.callee. nobody seems to talk about it, but as of now, you can even get the caller property:
Code:
function one(){
return two();
}
function two(){
return "two says: " + arguments.callee.caller.toString();
}
alert( one() )
if it's undefined, guess where the function was called from?
ecma5 cleans up a lot of this mess, along with inconsistencies resulting from code like "var arguments" or "return arguments".
i encourage everyone to read the spec, and not just the new parts.
you will find a lot of familiar routines have the same name, but different and more specific steps.
Bookmarks