新增加的语法糖,作用:让对象原型的写法更加清晰,更像面向对象的编程方式;构造函数的另一种写法
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、通过class面向对象
class Person1{
constructor(name,age){
this.name = name;
this.age = age;
}
print1(){
console.log('我叫' + this.name + ',今年' + this.age + '岁');
}
}
let person1 = new Person1('张三',19);
console.log(person1);
person1.print1();
网友评论