What does "self" mean in javascript?
Asked Answered
B

5

35

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?

Buss answered 9/7, 2010 at 20:45 Comment(5)
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")Curry
Thanks for the clarification. i have never used js outside of the DOM.Buss
Never trust HTML Goodies, it is awful. It makes W3Schools look good.Beebread
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.Buss
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.Anastigmat
A
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.

Altarpiece answered 9/7, 2010 at 20:51 Comment(2)
so I guess self == window. The link I posted is very misleading. If that is a quote could you post the source?Buss
Original source docs.sun.com/source/816-6408-10/window.htm. Modern source dev.w3.org/html5/spec/Overview.html#dom-selfAnastigmat
A
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
Ailanthus answered 10/7, 2010 at 5:3 Comment(0)
A
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.

Altarpiece answered 9/7, 2010 at 20:51 Comment(2)
so I guess self == window. The link I posted is very misleading. If that is a quote could you post the source?Buss
Original source docs.sun.com/source/816-6408-10/window.htm. Modern source dev.w3.org/html5/spec/Overview.html#dom-selfAnastigmat
M
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();
Myalgia answered 9/7, 2010 at 20:55 Comment(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); };Atalayah
P
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

Plead answered 25/3, 2018 at 22:52 Comment(0)
M
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.

Merideth answered 9/7, 2010 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.