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