-
Notifications
You must be signed in to change notification settings - Fork 403
Closed
Labels
wontfixWe decided not to fix this issue/not implement that feature request.We decided not to fix this issue/not implement that feature request.
Description
In ES6 classes, static properties should be inherited. That is,
class Foo {
}
Foo.x = 5;
class Bar extends Foo {
}
console.log(Bar.x);should log "5". This can be done by looping over the properties and copying, but that's subpar because updating the static properties on the superclass should update them on the subclass too. Babel sets __proto__ to achieve this:
It doesn't look like the ES5 emitter makes any attempt to get the same behavior:
scala-js/tools/shared/src/main/scala/org/scalajs/core/tools/javascript/ScalaJSClassEmitter.scala
Lines 202 to 257 in bb2e2a7
| /** Generates the JS constructor for a class, ES5 style. */ | |
| private def genES5Constructor(tree: LinkedClass): js.Tree = { | |
| implicit val pos = tree.pos | |
| val className = tree.name.name | |
| val isJSClass = tree.kind.isJSClass | |
| def makeInheritableCtorDef(ctorToMimic: js.Tree) = { | |
| js.Block( | |
| js.DocComment("@constructor"), | |
| envFieldDef("h", className, js.Function(Nil, js.Skip())), | |
| js.Assign(envField("h", className).prototype, ctorToMimic.prototype) | |
| ) | |
| } | |
| val ctorFun = if (!isJSClass) { | |
| val superCtorCall = tree.superClass.fold[js.Tree] { | |
| js.Skip() | |
| } { parentIdent => | |
| js.Apply( | |
| js.DotSelect(encodeClassVar(parentIdent.name), js.Ident("call")), | |
| List(js.This())) | |
| } | |
| val fieldDefs = genFieldDefs(tree) | |
| js.Function(Nil, js.Block(superCtorCall :: fieldDefs)) | |
| } else { | |
| genConstructorFunForJSClass(tree) | |
| } | |
| val typeVar = encodeClassVar(className) | |
| val docComment = js.DocComment("@constructor") | |
| val ctorDef = envFieldDef("c", className, ctorFun) | |
| val chainProto = tree.superClass.fold[js.Tree] { | |
| js.Skip() | |
| } { parentIdent => | |
| val (inheritedCtorDef, inheritedCtorRef) = if (!isJSClass) { | |
| (js.Skip(), envField("h", parentIdent.name)) | |
| } else { | |
| val superCtor = genRawJSClassConstructor( | |
| linkedClassByName(parentIdent.name)) | |
| (makeInheritableCtorDef(superCtor), envField("h", className)) | |
| } | |
| js.Block( | |
| inheritedCtorDef, | |
| js.Assign(typeVar.prototype, js.New(inheritedCtorRef, Nil)), | |
| genAddToPrototype(className, js.Ident("constructor"), typeVar) | |
| ) | |
| } | |
| val inheritableCtorDef = | |
| if (isJSClass) js.Skip() | |
| else makeInheritableCtorDef(typeVar) | |
| js.Block(docComment, ctorDef, chainProto, inheritableCtorDef) | |
| } |
This means scala-js does not work with React 0.14+ which checks for a static flag on classes extending React.Component:
Metadata
Metadata
Assignees
Labels
wontfixWe decided not to fix this issue/not implement that feature request.We decided not to fix this issue/not implement that feature request.