ES5中的面向对象
//1.构造函数
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype = {
constructor:Person,
print(){
console.log("我叫" +this.name +',今年' +this.age +'岁');
}
}
let person =new Person('张三',19);
console.log(person);
//2.ES6通过class面向对象
class Person {
constructor(name,age){
this.name = name;
this.age = age;
}
print(){
console.log("我叫" +this.name +',今年' +this.age +'岁')
}
}
let person =new Person('张三',19);
console.log(person);