1.创建类
class Person {
constructor(name, age) {
this.name = name
this.age = name}
}
2.创建实例
var Person = new Person('new',18)
console.log(Person.name)
console.log(Person.age)
constructor:对象的constructor属性用于返回创建该对象的函数,也就是我们常说的构造函数。
构造函数的特点:
a:构造函数的首字母必须大写,用来区分于普通函数
b:内部使用的this对象,来指向即将要生成的实例对象
c:使用New来生成实例对象
prototype:属性使您有能力向对象添加属性和方法。
object.prototype.name = value
object.constructor
在JavaScript语言中,constructor属性是专门为function而设计的,它存在于每一个function的prototype属性中。这个construtor保存了指向function的一个引用。