原型链直接继承
function Person() {}
Person.prototype.foot = 2;
Person.prototype.head = 1;
Person.prototype.name = "老哥";
function Student(name, no) {
this.name = name;
this.no = no;
}
function extend (Child ,Parent){//封装
var O = function() {} ; //中间变量
O.prototype = Parent.prototype ;//继承来自parent的所有东西
Child.prototype = new O();//new出来的新对象的所有方法给child
Child.prototype.constructor = Child ;//把child手动指回来
}
extend(Student,Person) ;
var stu = new Student("老杨", "22");
alert(stu.name);
alert(stu.head);
原型链直接继承prototype
function Person() {}
Person.prototype.foot = 2; //this 指代Person
Person.prototype.head = 1;
Person.prototype.name = "老哥";
function Student(name, no) {
this.name = name;
this.no = no;
}
Student.prototype = Person.prototype;
Student.prototype.constructor = Student;
var stu = new Student("老杨", "22");
alert(stu.name);
alert(stu.head);
原型链继承_prototype属性
function Person() {
this.foot = 2; //this 指代Person
this.head = 1;
this.name = "老哥";
}
function Student(name, no) {
this.name = name;
this.no = no;
}
Student.prototype = new Person();
//student 也具有了food和head (赋值)
//添加了新的内容 但是指向未变 未告诉原型
// var a = 4 ;
// a = a+1;
Student.prototype.constructor = Student;
//告诉原型我加了新的属性
var stu = new Student("老杨", "22");
alert(stu.constructor);
alert(stu.name);
//若有冲突,加载原函数的的属性
继承_构造函数绑定
function Person(){
this.foot = 2 ;
this.head = 1 ;
this.favorColor = ["red" , "yellow"] ;
}
function Student(name,no) {
// Person.call(this) ;
Person.apply(this) ;
this.name = name ;
this.no = no ;
}
var stu = new Student ("大师" , "11") ;
stu.favorColor.push("greed") ;
alert(stu.favorColor) ; //red,yellow,greed
var stu1 = new Student("老杨" ,"22") ;
alert(stu1.favorColor) ; //red,yellow
网友评论