Is it possible for a piece of JavaScript code to refer to itself? That is, can I programmatically build a variable such that its value is the raw text content of the JavaScript file declaring it?
-
The jQuery $ slector is refering to jQuery itself. So, shortly - yes. – Bakudan Sep 17 '11 at 03:16
-
@Milo ...what? OP: could you explain what you're trying to do? JavaScript isn't exactly made for metaprogramming. – Matt Ball Sep 17 '11 at 03:18
-
@Matt IMO that depends on what you mean by "metaprogramming"; the combination of prototypes and `eval` ain't bad. – Dave Newton Sep 17 '11 at 03:25
-
If using '$' as a selector, I would expect $('$') to reference an array of elements with a tag name of "$", which is not a valid tag name in HTML or XML and therefore should return a jQuery object with no elements. The use of $ as reference to "jQuery itself" is just an example of a global variable referencing an object. – RobG Sep 17 '11 at 03:25
-
@Matt Ball from jQuery development version the line before the last one "window.jQuery = window.$ = jQuery;" it is refereing to itself, isn`t it? – Bakudan Sep 17 '11 at 03:30
-
Curious, what is your need for this again? – vol7ron Sep 17 '11 at 03:39
-
@vol7ron: I need to satisfy my curiosity. – Randomblue Sep 17 '11 at 03:41
-
The term being looked for is [quine](http://en.wikipedia.org/wiki/Quine_%28computing%29): A quine is a computer program which takes no input and produces a copy of its own source code as its only output. (Quines can be written without the use of reflection such as that found in the `toString` answers). – Sep 17 '11 at 04:22
3 Answers
Try,
(function foo() { var f = foo.toString(); return f; })()
where f
is a variable as you describe. This also returns itself as string just for good measure.

- 27,773
- 7
- 53
- 49
-
Be very careful with that. In a function expression, the optional name is treated differently by different browsers: in IE, the expression is treated as a function declaration so afterward there is a global variable *foo* that references the function. In other browsers, the function name is only accessible from within the function, no global variable persists (it likely is created as a property of the local variable object). To ensure consistency across browsers, it is better to use an anonymous function and *arguments.callee* to reference the function from within. – RobG Sep 22 '11 at 01:02
-
Be aware that `arguments.callee` is deprecated and is not legal to access in strict mode which is why I didn't use it above. – chuckj Sep 22 '11 at 06:28
-
chuckj - *callee* is not deprecated, it is defined in ES 5 and Mozilla's [JavaScript reference](https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments#Properties), you might be getting confused with the *caller* property. It isn't available in strict mode, but that isn't viable for the general web anyway. – RobG Sep 23 '11 at 00:26
-
Please read the ECMA standard section 10.6 step 14.c. NOTE 3 of that section describes what is doing in prose. – chuckj Sep 23 '11 at 02:39
-
As stated, I considered both ES5 and Mozilla's JavaScript Reference when replying. Neiter document says that *callee* is deprecated, or that there is any intention to do so in future. The MDC documentation says that *caller* is obsolete, its inclusion in ES5 seems to be specifically designed to stop its resurrection. Regardless, *callee* is not deprecated in either the standard or JavaScript™. – RobG Sep 23 '11 at 05:02
-
If you use `callee` in a strict mode function it will throw a type error as required by 10.6 step 14.c of the ES5 standard. All implementations I have tested (FF, Chrome, IE10) all throw a type error. You are correct that outside a strict mode function `callee`'s semantics are clearly spelled out in the ES5 standards in 10.6 step 14.b but is just as clearly poisoned in 14.c. They don't use the term depricated but that is clearly the intent. – chuckj Sep 27 '11 at 03:23
-
If the authors had wanted to deprecate it they would have done so. I can't see that intention either explicity or tacitly. – RobG Sep 27 '11 at 05:56
-
They did. They made it throw an exception. Any code that uses `callee` will throw an exception in strict mode. That is the ES5 strict mode pattern for depricating such features. ES5 could not have make this an early error, the other pattern, becuase there are just too many ways to defeat an early error for property access. – chuckj Sep 28 '11 at 01:49
-
No one uses strict mode on the web, and no one is likely to for a very long time except for very specific cases (I can't think of any). It is not a proxy for deprecation—as I said earlier, if the authors had wanted to deprecate it they would have done so explicitly in the specification. – RobG Sep 28 '11 at 05:53
-
I apologize for using the word deprecated which clearly has a very specific meaning to you. The ES5 sepcification is discouraging the use of callee by causing it to throw an exception in conforming strict mode implementations. You are free to continue to use callee but such code will not work in strict mode. That was my original point and why I didn't use arguments.callee. – chuckj Oct 01 '11 at 06:33
(function () { return arguments.callee.toString(); })()
Almost the same thing as the selected answer, w/o the variable storage of the string, or the function name. Neither really refers to itself. You can't do something like $(this).closest('script')
to refer to the JS file in the DOM.
What you can do is something similar to this:
var w = window;
w.stored = [];
w.scripts = document.getElementsByTagName('script');
w.stored.push(w.scripts[ w.scripts.length - 1 ]);
As stated, this can be done because the DOM loads scripts sequentially. You can refer to all the scripts that did this like w.stored[0]
. Using jQuery you could do $(w.stored[0]).text()
JSFiddle Demo
Is it possible for a piece of JavaScript code to refer to itself?
It is possible for a function to refer to itself using arguments.callee (ECMA-262 10.6). I think it's not available in strict mode but that shouldn't be too much of an issue. You can call the function's toString method to get an implementation-dependent representation of the function
, which is typically a string equivalent to the source code that created it, but it may not be.
That is, can I programmatically build a variable such that its value is the raw text content of the JavaScript file declaring it?
Not in a general sense, e.g. you can't do:
var x = 'x';
getVaue(x); // var x = 'x';
if that is what you mean.

- 142,382
- 31
- 172
- 209
-
I could just wrap everything in a self-executing function as done above. Thanks. – Randomblue Sep 17 '11 at 03:40