在ES6中如何在派生类中创建基类函数`null`?

时间:2016-02-04 19:35:14

标签: javascript ecmascript-6

当我将课程从@Override public boolean equals(Object o1) { if (this == obj) { return true; } Fruit fruit = (Fruit)o1 if (name != null && fruit.getName() != null) { if (name.equas(fruit.getName())) { return true; } return false; } return false; 派生到Animal时,我想制作Dog函数speak, 在我们可以通过原型null完成之前如何在Dog.prototype.speak = null

中实现这一目标
es6

2 个答案:

答案 0 :(得分:2)

您可以在构造函数中设置它:

apache-poi

请注意,这不会将import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTxbxContent; import schemasMicrosoftComVml.CTGroup; import schemasMicrosoftComVml.CTShape; import schemasMicrosoftComOfficeWord.CTWrap; import schemasMicrosoftComOfficeWord.STWrapType; /* To import schemasMicrosoftComOfficeWord.CTWrap; import schemasMicrosoftComOfficeWord.STWrapType; ooxml-schemas-1.1.jar is needed - available from http://search.maven.org/#artifactdetails|org.apache.poi|ooxml-schemas|1.1|jar */ import org.w3c.dom.Node; public class CreateWordTextBox { public static void main(String[] args) throws Exception { XWPFDocument doc= new XWPFDocument(); XWPFParagraph paragraph = doc.createParagraph(); XWPFRun run=paragraph.createRun(); run.setText("The Body text: "); CTGroup ctGroup = CTGroup.Factory.newInstance(); CTShape ctShape = ctGroup.addNewShape(); ctShape.addNewWrap().setType(STWrapType.SQUARE); ctShape.setStyle("position:absolute;mso-position-horizontal:center;margin-top:40pt;width:100pt;height:24pt"); CTTxbxContent ctTxbxContent = ctShape.addNewTextbox().addNewTxbxContent(); ctTxbxContent.addNewP().addNewR().addNewT().setStringValue("The TextBox text..."); Node ctGroupNode = ctGroup.getDomNode(); CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode); run=paragraph.createRun(); CTR cTR = run.getCTR(); cTR.addNewPict(); cTR.setPictArray(0, ctPicture); paragraph = doc.createParagraph(); run=paragraph.createRun(); run.setText("Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... Lorem ipsum semit color ... "); doc.write(new FileOutputStream("WordTextBox.docx")); } } 设置为null,但会在Dog的实例上设置属性。

class Dog extends Animal {
    constructor() {
        super();
        this.speak = null;
    }
}

答案 1 :(得分:2)

从子类(或子类的单个实例)中删除方法是一种特别危险的反模式。如果没有speak方法,Dog不再扩展Animal,但原型链声称它会。这会误导Animal的任何用户,他们可能会尝试拨打animal.speak()并突然发现自己的Uncaught TypeError: animal.speak is not a function

将该功能重新定义为无操作:

class Dog extends Animal {
  speak() {
    // noop: dogs can't speak
  }
}

或者,如果超类允许它,请将其抛出:

class Dog extends Animal {
  speak() {
    throw new Error("Dogs can't speak.");
  }
}

实际上使函数为null是一种反模式,因为Animal接口的每个使用者都需要知道某些方法可能不存在,这会破坏继承的概念(子项可以添加但不会带走)。

让子类抛出错误的操作是一种常见的做法,但将函数转换为noop(静默跳过)也是有效的。在JS中,考虑到JIT编译器的工作原理,noop函数应该被省略而不是实际调用,因此不应该对性能产生影响(或者非常非常少)。