34

I read here that "self Refers to the current window or form".

Self does not seem to refer to the current form in this case:

<form><input type="text" onkeyup="alert(self.foo.value)" name="foo"></form>

However in this case it works (referring to the window):

<form><input type="text" onkeyup="alert(self.document.forms[0].foo.value)" name="foo"></form>

So when would you use the self DOM property over just window?

laconbass
  • 17,080
  • 8
  • 46
  • 54
Nick Van Brunt
  • 15,244
  • 11
  • 66
  • 92
  • 11
    I'd just like to point out that self is not a keyword, to avoid confusion. self is a property of window (properties are members of objects, keywords are essentially what makes up the programming language itself, such as "if" and "for") – Jani Hartikainen Jul 09 '10 at 20:49
  • 1
    Thanks for the clarification. i have never used js outside of the DOM. – Nick Van Brunt Jul 09 '10 at 20:52
  • 17
    Never trust HTML Goodies, it is **awful**. It makes W3Schools look good. – Quentin Jul 09 '10 at 20:58
  • I modified the question in light of answers pointing out self is not in fact a key word. I'm still curious why it exists. – Nick Van Brunt Jul 09 '10 at 21:02
  • 2
    I've no idea where they got the idea that the `self` global (`window` property) would ever point to a form. It's utter tosh. Maybe they got it confused with what `this` points to in `form onsubmit`. – bobince Jul 09 '10 at 21:15

5 Answers5

15

Other replies have pointed out that self is not going to refer to the FORM and that self is window. They're right; self is window. Well, except when it isn't. In either IE6 or IE7 (forgot), self.onload would not fire, though window.onload would.

All official versions of IE (and even IE9pr3) have an odd, intransitive implementation of == with these host objects. Using == to compare either window or self to a node in the document, the result is true.

IE Oddities

alert(self == document.body); // true
alert(document.body == self); // false
alert(window == self); // true
alert(window === self); //false
var b = document.createElement("b");
alert(window == b); // false
alert(window == document.body.appendChild(b)); // true
alert(window == document.body.removeChild(b)); // false
Garrett
  • 2,936
  • 1
  • 20
  • 22
14

For all windows, the self and window properties of a window object are synonyms for the current window, and you can optionally use them to refer to the current window. For example, you can close the current window by calling the close method of either window or self. You can use these properties to make your code more readable or to disambiguate the property reference self.status from a form called status.

dakab
  • 5,379
  • 9
  • 43
  • 67
Swift
  • 13,118
  • 5
  • 56
  • 80
  • so I guess self == window. The link I posted is very misleading. If that is a quote could you post the source? – Nick Van Brunt Jul 09 '10 at 20:54
  • 5
    Original source http://docs.sun.com/source/816-6408-10/window.htm. Modern source http://dev.w3.org/html5/spec/Overview.html#dom-self – bobince Jul 09 '10 at 21:13
11

self is not a reserved keyword or standard type, but has become a defacto standard name when for keeping reference to an object for closures.

Here we create a closure to pass to setTimeout(). When that closure is executed, this will refer to the global object. To keep a reference to the foo object doLater was originally called on, a var named self is used. It could be anything but 'self' has meaningful semantics.

Foo.prototype.doLater = function() {
  var self = this; // current object

  window.setTimeout(function() { self.doSomething(); }, 1000);
}

new Foo().doLater();
Chadwick
  • 12,555
  • 7
  • 49
  • 66
  • 1
    While this works, I suggest that the more idiomatic JavaScript solution would be `Foo.prototype.doLater = function() { setTimeout(function() { this.doSomething(); }.bind(this), 1000); };` – ic3b3rg Oct 21 '14 at 07:32
10

self refers to the global scope - If the context is a window it will refer to window.self, while in case of a non-windowed context it will refer to the global scope of that context (e.g. in service worker code, self refers to the worker global scope).

self refers to the global scope of a context:

  • window context
  • worker context (support for multiple workers for the same webapp is being worked on).

https://developer.mozilla.org/en-US/docs/Web/API/Window/self

6

Never, unless I wanted to create a self variable in the scope of a function referring to the context for later reference,

function Constructor() {
    var self = this;
}

You should use this to refer to the element at hand, not self. In global scope though this is a reference to window, and self is also window.

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434