4 ms·
It's whatever is to the left of the dot. The exceptions: 1) when used with new, it's the new instance of the object (pretend that when you use the new keyword
by brianchu 12y ago
It's whatever is to the left of the dot.
The exceptions:
1) when used with new, it's the new instance of the object (pretend that when you use the new keyword, an Object.create() call is inserted in the beginning of the constructor function),
2) when there's nothing to the left of the dot, it's the window object.
3) .call and .apply force this to be something else.
- DonHopkins 12y agoUNLESS somebody calls your constructor as a function, instead of with "new". It breaks my heart to see constructors that check to see if they were called as a function and then try to do the right thing by recursively calling themselves with "new". There is no possible reason for calling a constructor as a function -- it is always the wrong thing to do, so it should be impossible, not tempting. Conflating functions with constructors was stupid. Why should there only be one possible constructor for a class, anyway? And it's a terrible idea for constructors to try to silently correct mistakes that programmers make calling them in the wrong way, instead of asserting an error, because you know damn well those programmers are making that same mistake all over the place, and lots of other constructors don't try to correct the mistake of calling them directly as functions. The idea of using functions as constructors, but requiring them to be called with "new", yet still allowing those functions to be called as normal functions, which seems like a very reasonable thing to do, especially if you're coming from Python, is the other incredibly idiotic JavaScript design flaw, up there with "this". It's like having a gaping wound that never heals and is just waiting for another infection to come along. The other stupid thing about JavaScript is the idea that a subclass should inherit from an INSTANCE of its superclass, instead of from the superclass directly. Why insert one extra level of indirection and waste the time and memory and simplicity? And why require that the constructor of all classes that you might ever subclass be callable with no arguments to create a dummy intermediate instance through which a subclass can inherit its class? That puts restrictions on and adds needless complexity to how constructors work. JavaScript was NOT a clearly thought out language, and it's insulting to Self to claim that Self was its inspiration, when there's no good way to dynamically change the prototypes an object inherits from at runtime, and an object can only inherit from one prototype. Self is SO SIMPLE and SO EASY to understand, yet JavaScript is so confusing, that comparing it to Self just sets you up to be surprised and disappointed if you know Self, or think badly of Self if you only know JavaScript. And don't even get me started about JavaScript's and Brendan Eich's misconceptions about equality. It's really a shame, because the fuzzy magical thinking that went into JavaScript made it a lot more complex and error prone that it would have been otherwise, if only it had been designed by somebody who knew what they were doing and learned from other language's mistakes without aping other language's mistakes, instead of having such a cargo cult design, and then having a cargo cult name slapped on to mislead people into thinking it had something to do with Java, which WAS written by somebody who knew what they were doing.
- lumpysnake 12y agoMost of your explanation does not translate really well outside of javascript. Where did you get that the OP is using javascript?