1

I have the hang of basic javascript syntax and am working to understand the language at a deeper level. I am looking at this line of code from the elizabot.js file in the elizabot.js library:

var global=ElizaBot.prototype.global=self;
  1. I think that this is setting the global property of the prototype for the Elizabot object equal to "self." Am I understanding the meaning of that line correctly?
  2. Self does not seem to be a reserved word in javascript. But if I search the Elizabot.js file for the word "self" I can't find it. Is there some special meaning for the word self in javascript? I can't find the declaration.
bernie2436
  • 22,841
  • 49
  • 151
  • 244
  • Check out this http://stackoverflow.com/questions/3216428/self-property-in-javascript – Carth Sep 26 '13 at 21:07
  • 1
    I just popped open the console and typed "self" and it returned the Window. I'm using Chrome, so I'm not sure if that's standard across the board, but yeah! Apparently it is defined. – callmehiphop Sep 26 '13 at 21:08
  • `self` is refering to `window.self`: https://developer.mozilla.org/en-US/docs/Web/API/window.self. Here's the docs about `window`: https://developer.mozilla.org/en-US/docs/Web/API/Window – gen_Eric Sep 26 '13 at 21:11
  • self is typically the only global handle when window is not present like in node.js, webwrokers, etc. – dandavis Sep 26 '13 at 21:15
  • @dandavis ```self``` is undefined in nodejs. nodejs uses ```global``` for the global namespace object – cbayram Sep 26 '13 at 21:21
  • @cbayram: you're right. i must have been thinking about something else... still webworkers alone makes it more valuable than window. – dandavis Sep 26 '13 at 21:29

2 Answers2

4

In the browser, self refers to the global window object

ElizaBot is a function that is setting its global property to the window object for all instances of ElizaBot created (through prototype)

var global = ElizaBot.prototype.global=self;
function ElizaBot(){
  console.warn(this.global == self);
  console.warn(this.global == window);
  console.warn(self == window);
  console.warn(self.self.self.window.self == this.global.self.window);
  // I can go on forever :) time to stop
}
new ElizaBot();
new ElizaBot();
cbayram
  • 2,259
  • 11
  • 9
  • Apparently, and much to my surprise, you're right (in Chromium 28/Ubuntu 12.10 at least). – David Thomas Sep 26 '13 at 21:09
  • @cbayram and it is also setting the value of the var global to the global property of the eliza function, right? There are two assignments going on in that one line of code. – bernie2436 Sep 26 '13 at 21:33
  • @abe3 yes ```var global``` is also set to self/window/ElizaBot.prototype.global object, all which are references to the same object. – cbayram Sep 26 '13 at 21:37
2

In a browser (not in generic javascript), self is a property of the window object that contains the value of the current window. Since all properties of the window object are accessible as global variables, you can refer to just self and it will contain a reference to the current document's window.

Thus, in your code:

var global=ElizaBot.prototype.global=self;

is assigning to both ElizaBot.prototype.global and to the variable global a reference to the current window object.

See here for info on window.self which is also accessible as just self.


FYI, all of these assignments seem superfluous since the current window object is generally accessible in other ways and need not be stored separately.

jfriend00
  • 683,504
  • 96
  • 985
  • 979