5 ms·
The "classes" are merely a short-hand for creating constructor functions with their prototype property already assigned: they add no new semantic complexity to
by gsnedders 14y ago
The "classes" are merely a short-hand for creating constructor functions with their prototype property already assigned: they add no new semantic complexity to the language, they are mere syntax.
- TazeTSchnitzel 14y agoYeah, they're syntactic sugar.
- Offler 14y agoI'd bet you they will allow browsers to optimize their compilation steps, so they are more then just sugar.
- TazeTSchnitzel 14y agoNo, they're just sugar. It has no relation to whether you can use them to optimise things or not.
- Offler 14y agoIt's called Type Specialization http://dl.acm.org/citation.cfm?id=1542528 http://dl.acm.org/citation.cfm?id=1542528
- asynchrony 14y agoClass declarations will be equivalent (can be desugared to) a function declaration and some manipulations of the function's prototype. JS VMs already optimize this pattern, so why should syntax sugar for classes be anything special for the compiler?
- Offler 14y agoBecause it takes time and CPU cycles for them to figure out what is a Type and what is not. They don't know how much memory to allocate to these objects for instance, but now with classes they will know types immediately.
- wycats 14y agoThey also add class-side inheritance. This allows the constructor to inherit its properties from a parent constructor: class Class {} Class.hello = "hello"; class SubClass extends Class {} SubClass.hello // "hello" This doesn't add any new semantics on top of the already-approved `__proto__` (which already exists in all browsers but IE): Class = function() {}; Class.hello = "hello"; SubClass = function() {}; SubClass.__proto__ = Class; SubClass.hello // "hello" EDIT: Oh, and they add `super`: class Speaker { say(string) { console.log(string); } } class Yeller extends Speaker { say(string) { super(string.toUpperCase()); } } new Yeller().say("hi") // "HI"
- gsnedders 14y agoAh, I'd forgotten it now mutated [[Prototype]] and not the prototype property's [[Prototype]] (IIRC, earlier drafts had it mutating the latter, i.e., `SubClass.prototype.__proto__`). `super` is indeed new semantics though, so that is definitely true.
- delaaxe 14y agoAbout the last example, I think you meant : super.say(string.toUpperCase()); See: http://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_classes http://wiki.ecmascript.org/doku.php?id=strawman:maximally_mi...