1

I am confused about object-oriented programming and inheriting in Javascript.

I think studying the following code will be a good way to understand the inheritance in Javascript. The class B both inherits and overrides properties of the base class A.

So could anyone please show me a concise equivalent of the following Java code in Javascript

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";
    }
}

so that the following Javascript code gives the corresponding output

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
Firas
  • 467
  • 4
  • 15

1 Answers1

0

ECMA5:

There are no way to deep-clone object. So you can't inherit it.

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

Community
  • 1
  • 1
Earthchie
  • 54
  • 5