原型对象:
我们创建的每一个函数都有一个原型属性(pope),这个属性是一个指针指向一个对象,这个对象有自己的特定类型
的供所有实例是使用的属性和方法,那么prototype就是通过调用构造函数而创建的那个原型对象
理解:
只要创建了一个新函数就会为该函教创建个protoptype属性,这个属性指向原型对象
所有的原型对象都会自动获得constructor(构造函数)属性,这个属性包含指向protoptype属性的指针
function student(iname, iclass, iage, inum) {
this.name = iname;
this.class = iclass;
this.age = iage;
this.num = inum;
this.xin = function () {
var arr = [
"姓名: "+this.name,
"年龄: "+this.age,
"班级: "+this.class,
"成绩: "+this.num
]
alert(arr)
}
}
var oname1 = new student("张三", 20, "3班",90);
var oname2 = new student("李四", 19, "2班",70);
oname1.xin();
oname2.xin();
原型链
原型链:
function Text() {
}
Text.prototype.name = "lily";
Text.prototype.courses = ["html", "css"];
Text.prototype.showMsg = function () {
console.log(this.name);
console.log(this.courses);
}
var t1 = new Text();
t1.name = "tom";
t1.courses.push("js");
t1.showMsg();
t2.showName();
//操作过成:
// 1、如果操作基本数据类型,复合Text原型对象的基本数据name,把修改过得值存储在自己的原型对象
//2、如果复合数据类型(引用)比如数组,对象,方法,那么不会复刻到自己的原型对象里,
通过原型链,直接修改父亲原型对象内容