class Student{ constructor (name,age){ this.name=name; this.age=age; } run(){ return this.name; } } let xs = new Student("姜姜",23); console.log(xs.name); console.log(xs.age); function Vehicle (name,age){ this.name = name; this.age = age; } Vehicle.prototype.name = function name(){ return this.name; }; var jj = new Vehicle ("吴建",24); console.log(jj.name);
class Student{ constructor (name,age){ this.name =name; this.age=age; } run(){ console.log("我会飞"); } get xm(){ return this.name +"123"; } set xm(value){ this.name =value; } static shangxue (){ console.log("去上学"); } } let xs = new Student("姜姜",25); console.log(xs.xm); xs.xm="姜姜"; console.log(xs.xm); Student.shangxue(); //get:获取加赋值。 //set:设置。 //static:静态方法|类方法。 //set和get的方法名相同,并且能够同名
class Student{ constructor (name,age){ this.name =name; this.age=age; } run(){ console.log("我会飞"); } } let xs = new Student("姜姜",25); class Teacher extends Student{ constructor (name,age,sex){ super(name,age); this.sex=sex; } eat(){ console.log(this.name +"is eating") } run(){ super.run(); console.log("我想高飞"); } } var ls = new Teacher("吴建","30","男"); ls.run();//我会飞 我想高飞; 注释:虽然子类继承了父类的run方法,可是子类会把父类的方法给覆盖掉,这个就是方法覆盖
非声明提高(hoisted) 和let同样javascript
自动处于严格模式java
须要new, 不然会抛错es6
重写类名和方法会抛错函数
有get set 方法this
能够指定方法为static 。只能在class内部使用。prototype