var per=new Person("武凤楼",25,"魏荧屏"); //输出:“武凤楼” per.say(); var stu=new student("曹玉",18);//输出:“曹玉” stu.say(); var tea=new teacher("秦杰",16);//输出:“秦杰” tea.say();
</script> </body> </html>
有如下代码:
1 2 3 4 5 6 7 8 9
var A = function(x) { this.x = x; } A.prototype.say = function() { console.log(this.x); } a = new A(3); a.say(); console.log(a);
如果改成es6的写法:
class A {
constructor(x){
this.x = x;
}
say(){
console.log(this.x);
}
}
我们要定义一个B,让B继承A的方法;
1 2 3 4 5 6 7 8
// B继承A var B = function(x) { A.call(this, x) // 继承x } B.prototype = new A(); // 继承A的原型 var b = new B(5); b.say(); console.log(b);
如果写成es6语法的继承: // C继承A
class C extends A{};
var c = new C(6);
c.say();
console.log(c);
把数字转换成数组
class NumPrototype {
constructor(x) {
this.x = x;
}
get iterate() {
return this.iterateF();
}
iterateF () {
var result = [];
for (var i = 0; i <= this.x; i++) {
result.push(i);
}
return result;
}
};
var no = new NumPrototype(8);
var arr = no.iterate;
console.log(arr, no.iterate);