构造函数里new关键字的执行过程

1.new构造函数在内存中创建了一个空的对象

2.this就会指向创建的空对象

3.执行构造函数里面的代码,添加属性和方法

4.返回这个对象

function Person(uname,age,sex){

this.name = uname;

this.age = 18;

this.sex = sex;

this.say = function(talk) {

console.log(talk)

}

}

var allen = new Person(‘艾伦’,18,‘女’);

console.log(allen.name);

console.log(allen[‘age’]);

console.log(allen.sex);

allen.say(‘hello’);

posted on 2020-09-29 11:51  sbird  阅读(290)  评论(0)    收藏  举报

导航