如何设置默认值的属性p1,p2,p3和一类p4`:
class O extends AnotherClass {
constructor(p1,p2,p3,p4) {
super(); \\ for the this-reference
if (p1) this.p1 = p1;
if (p2) this.p2 = p2;
if (p3) this.p3 = p3;
if (p4) this.p3 = p4;
}
Run Code Online (Sandbox Code Playgroud)
我必须一个一个地写
O.prototype.p1 = "default1"
O.prototype.p2 = "default2"
O.prototype.p3 = "default3"
O.prototype.p4 = "default4"
Run Code Online (Sandbox Code Playgroud)
还是有更优雅的方式,比如
O.prototype = {p1: "default1", p2 : "default1", p3 : "default3", p4 : "default4"}
Run Code Online (Sandbox Code Playgroud)
但后者似乎不起作用......