我对 Javascript 中的面向对象编程和继承感到困惑。
我认为研究下面的代码将是理解 Javascript 中的继承的好方法。类 B 继承并覆盖基类 A 的属性。
有人可以向我展示一下 Javascript 中的以下 Java 代码的简明等效内容吗
public class A {
public static String a = "a", b = "b";
public static String c() {
return "c";
}
public static String d() {
return "d";
}
public String e = "e", f = "f";
public String g() {
return "g";
}
public String h() {
return "h";
}
}
public class B extends A {
public static String a = "A";
public static String c() {
return "C";
}
public String e = "E";
public String g() {
return "G";
}
}
这样下面的Javascript代码就会给出相应的输出
var a = new A();
var b = new B();
console.log(A.a); // a
console.log(B.a); // A override static property
console.log(A.b); // b
console.log(B.b); // b inherit static property
console.log(A.c()); // c
console.log(B.c()); // C override static method
console.log(A.d()); // d
console.log(B.d()); // d inherit static method
console.log(A.e); // e
console.log(B.e); // E override non-static property
console.log(A.f); // e
console.log(B.f); // e inherit non-static property
console.log(a.g()); // g
console.log(b.g()); // G override non-static method
console.log(a.h()); // h
console.log(b.h()); // h inherit non-static method
最佳答案
ECMA5:
没有办法深度克隆对象。所以你不能继承它。
What is the most efficient way to deep clone an object in JavaScript?
ECMA6:
https://developer.mozilla.org/th/docs/Web/JavaScript/Reference/Classes#Sub_classing_with_extends
关于javascript - 如何在Javascript中继承静态和非静态属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40672580/