1. js使用函数来创建对象
js编程中:
es5中没有class类 只有function函数
es6中出现class 向后端靠近
function people(){
this.name="";
this.age="";
this.sex="";
this.sleep=function(){
return "睡觉";
}
this.eat=function(){
return "吃饭";
}
}
people(); //this指向window
//使用new关键字实例化一个新对象
//new里面在替换this指针 thi指向当前对象
var xiaoyu=new people();
console.log(xiaoyu);
函数people(),就是当前的构造函数,this此时指向当前实例对象xiaoyu
要调用属性和方法,直接用 对象.属性 ;对象.方法
console.log(xiaoyu.sleep());
1.实例对象的原型链=函数的原型对象
xiaoming.proto == people.prototype;
2. 原型对象的构造指向本身
people.prototype.constructor==people;
2.new 实例化原理
function people(name,age,sex)