var var_name = new class_name { }
class Class_name{ }
class Student{ constructor(name, age){ this.name = name; this.age = age; } }
var Student = class{ constructor(name, age){ this.name = name; this.age = age; } }
var obj_name = new class_name([arguements])
var stu = new Student('Peter', 22)
obj.function_name();
'use strict' class Student { constructor(name, age) { this.n = name; this.a = age; } stu() { console.log("The Name of the student is: ", this.n) console.log("The Age of the student is: ",this. a) } } var stuObj = new Student('Peter',20); stuObj.stu();
The Name of the student is: Peter The Age of the student is: 20
'use strict' class Example { static show() { console.log("static Function") } } Example.show() //invoke the static method
static Function
class child_class_name extends parent_class_name{ }
'use strict' class Student { constructor(a) { this.name = a; } } class User extends Student { show() { console.log("The name of the student is: "+this.name) } } var obj = new User('Sahil'); obj.show()
The name of the student is: Sahil
class Animal{ eat(){ console.log("eating..."); } } class Dog extends Animal{ bark(){ console.log("barking..."); } } class BabyDog extends Dog{ weep(){ console.log("weeping..."); } } var d=new BabyDog(); d.eat(); d.bark(); d.weep();
eating... barking... weeping...
方法名称必须与父类中的相同。
方法签名必须与父类中的相同。
'use strict' ; class Parent { show() { console.log("It is the show() method from the parent class"); } } class Child extends Parent { show() { console.log("It is the show() method from the child class"); } } var obj = new Child(); obj.show();
It is the show() method from the child class
super(arguments);
'use strict' ; class Parent { show() { console.log("It is the show() method from the parent class"); } } class Child extends Parent { show() { super.show(); console.log("It is the show() method from the child class"); } } var obj = new Child(); obj.show();
It is the show() method from the parent class It is the show() method from the child class