简体   繁体   English

ES6 js“with”关键字

[英]ES6 js “with” keyword

In the deep internet I encountered structure like class A extends B with C { ... } , I haven't link right now, it was like macro for class while extending abstract class. 在深度互联网中,我遇到类似class A extends B with C { ... }结构, class A extends B with C { ... } ,我现在没有链接,在扩展抽象类时它就像类的宏。

Is this in standard? 这是标准的吗? (I googled, only finding old with that is removed right now, chrome inline console throws error) (我用Google搜索,发现只有老with被去除,现在,铬直列控制台抛出错误)

No this is not a part of the ECMAScript 6 Classes standard. 不,这不是ECMAScript 6 Classes标准的一部分。 See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes 请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

The with keyword is used in some programming languages to signify multiple inheritance. with关键字在某些编程语言中用于表示多重继承。

Scala for example has traits which classes can extend from with the with keyword 例如,Scala具有哪些类可以使用with关键字扩展的特征

trait Drawable {
    def draw() { }
}

trait Cowboy extends Drawable {
    override def draw() { println("Bang!") }
}

trait Artist extends Drawable {
    override def draw() { println("A pretty painting") }
}


class CowboyArtist extends Cowboy with Artist

It's not standard. 这不标准。 From Annex A.4 - Functions and Classes , the syntax is 附件A.4 - 函数和类 ,语法是

ClassDeclaration[Yield, Default] :
    class BindingIdentifier[?Yield] ClassTail[?Yield]
    [+Default] class ClassTail[?Yield]

ClassExpression[Yield] :
    class BindingIdentifier[?Yield]opt ClassTail[?Yield]

ClassTail[Yield] :
    ClassHeritage[?Yield]opt { ClassBody[?Yield]opt }

ClassHeritage[Yield] :
    extends LeftHandSideExpression[?Yield]

So the ClassTail is the part which includes both the optional extends something and the class body. 所以ClassTail是包含可选的extends something和class体的部分。 And that something must be a LeftHandSideExpression , without any with in there. something必须是LeftHandSideExpression,没有任何with在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM