构造函数
2019-05-08 本文已影响0人
王帅同学
1 function Person(name, height) {
2 this.name = name;
3 this.height = height;
4 }
5
6 var boy = new Person('Keith', 180);
7 console.log(boy.name); //'Keith'
8 console.log(boy.height); //180
9
10 var girl = new Person('Samsara', 160);
11 console.log(girl.name); //'Samsara'
12 console.log(girl.height); //160
- 创建了一个构造函数Person,传入了两个参数name和height。构造函数Person内部使用了this关键字来指向将要生成的对象实例。
- 用new命令创建了boy跟girl两个实例
- 调用了构造函数
new Person
以后变成一个空对象