构造函数可以实例化对象

 构造函数可以实例化对象
* 构造函数中有一个属性叫prototype,是构造函数的原型对象
* 构造函数的原型对象(prototype)中有一个constructor构造器,这个构造器指向的就说自己所在的原型对象所在的构造函数
* 实例对象的原型对象(__proto__)指向的是该构造函数额原型对象
* 构造函数的原型对象(prototype)中的方法是可以被实例对象直接访问的

* 需要共享的数据可以写在原型中
* 不需要共享的数据可以写在构造函数中

//构造函数
function Student(name,age,sex) {
this.name=name;
this.age=age;
this.sex=sex;
}
//共享===》所有学生身高188,体重55,每天要敲50行代码,每天吃10斤西瓜

// //原型对象
// Student.prototype.height="188";
// Student.prototype.weight="55";
// Student.prototype.study=function () {
// console.log("要写50行代码");
// };
// Student.prototype.eat=function () {
// console.log("要吃10斤西瓜");
// };

//原型简单写法
Student.prototype={
//手动修改构造器的指向
constructor:Student, //========================
height:"188",
weight:"55",
study:function () {
console.log("要写50行代码");
},
eat:function () {
console.log("要吃10斤西瓜");
}
};

//实例化对象,并初始化
var stu=new Student("小黑",22,"男");
console.dir(Student);


function Animal(name,age) {
this.name=name;
this.age=age;
}
//原型中添加方法
Animal.prototype.eat=function () {
console.log("喜欢吃水果");
this.play();
};
Animal.prototype.play=function () {
console.log("喜欢玩荡秋千");
this.sleep();
};
Animal.prototype.sleep=function () {
console.log("睡着了");
};
var dog=new Animal("小明",6);
dog.eat();
原文地址:https://www.cnblogs.com/lujieting/p/10066975.html