构造函数
作者:
王帅同学 | 来源:发表于
2019-05-08 00:35 被阅读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
以后变成一个空对象
本文标题:构造函数
本文链接:https://www.haomeiwen.com/subject/bevffqtx.html
网友评论