4 ms·
If anyone wonders why every JavaScript framework under the sun rolls its own object system, and they're all incompatible, and almost nobody uses native JavaScri
by WilliamLP 17y ago
If anyone wonders why every JavaScript framework under the sun rolls its own object system, and they're all incompatible, and almost nobody uses native JavaScript alone anymore, this is one of the reasons.
- adolph 17y agoWould you expand on this? Most frameworks share some convenience functions that make working in js or DOM easier and put all of their functions within a particular namespace instead of spamming global. Whats the problem?
- erlanger 17y ago> rolls its own object system === "Provides convenience functions for working with prototype chains." That has nothing to do with what makes the this keyword special in JavaScript, beyond prototypes affecting this' properties.
- WilliamLP 17y ago> "Provides convenience functions for working with prototype chains." Yes, that's pretty much what an object system is, isn't it? It's some convenience behaviour, but it would be nice if the language had a built-in way of doing things that wasn't so inconvenient.
- erlanger 17y agoI wasn't disputing that, I'm saying it's irrelevant to the discussion (the commenter implied that prototype modification is related to how this is determined in a given function call).
- tolmasky 17y agoI think it's pretty relevant. For example, something that is absolutely trivial in other languages (making super calls), is incredibly difficult to implement generically in JS, partially because of the way this behaves. This is something every class system (and of course just using prototype chains alone) in JS seems to run into. We ran into it early with Objective-J, and then CoffeeScript had the same problem: http://news.ycombinator.com/item?id=1014524 http://news.ycombinator.com/item?id=1014524
- erlanger 17y agoTo clarify, you're saying super is difficult to implement in JavaScript? I think the power of JavaScript's design is borne out by how easily one can recreate classical inheritance in the language, a view shared by Crockford.
- tolmasky 17y agoI'm not sure I understand. I'm saying specifically that at the very least, this one feature of classical inheritance is not easy to implement and in fact almost always leads to bugs when people try to do it. The truth is that most of the interesting features of classical inheritance are pretty hard to implement: 1. super, as described above 2. private, protected, etc. Never seen this work in js with inheritance 3. inheriting class methods as well as instance methods If all you care about is the most superficial copying of a superclass' methods, then yeah of course its easy to do in JS, its essentially just prototypal inheritance again. Its arguably just as easy to implement this watered down form of classical inheritance in C as well (or in any language I guess): struct MySubclass { struct MySuperclass base; int newMember; } I don't think this is necessarily an argument for elegance however. The point is, if these are features that everyone has to implement, then at some point you have to say "maybe its not that everyone else is stupid and doesn't get it, but that its an actually worthwhile feature". In fact, they predicted this (which is why class and super are reserved words in JS despite not doing anything), and its supposedly expected to come in the next iteration of JS (as in, the one after the one currently not even implemented yet). So, in 50 years when we get that we'll finally have remedied this issue.
- erlanger 17y ago> 1. super, as described above Make the super property a reference to the parent prototype in your "extends" function. ExtJS does this with the "superclass" property. > 2. private, protected, etc. Never seen this work in js with inheritance What? This is basic stuff. Private: var fn = function() {}; Protected: this.fn = function() {}; Public: Cls.prototype.fn = function() {}; > 3. inheriting class methods as well as instance methods See above. May I refer you to http://www.crockford.com/javascript/inheritance.html http://www.crockford.com/javascript/inheritance.html ?
- axod 17y agoAlmost nobody? Seriously? I'd be interested to see some data to back that up. 'this' takes all of about 5 minutes to understand in Javascript. It's pretty logical and there aren't really any surprises. Same with the prototypal inheritance.
- vr 17y agoThe reason why some frameworks roll out custom object systems is because they try to make JavaScript look like a language with classes, "this" semantics isn't what's driving them. In a language with classes developers are used to "this" being an instance of the current class, and this expectation creates a consfusion in JavaScript. If you don't look at it through the prism of classes, the rule for "this" is pretty simple: "this" is the receiver. Create a function, apply it to any object, and that object will be the receiver available as "this". It's that simple. Now the only confusion left is who's the receiver. In "foo.bar()" like cases it's obvious, and the only remaining case (if you don't count the discouraged "with" statement) is "bar()" which calls a function on the global object (like in Python).
- natrius 17y agoYou've left out event handlers, which are another case in which `this` can get confusing.