What does “self” mean in javascript?

前端 未结 5 1841
-上瘾入骨i
-上瘾入骨i 2020-12-02 23:04

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:

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 23:47

    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
    

提交回复
热议问题