javascipt 계승 실현
// call /apply ,
function person(name,age){
this.name=name;
this.age=age;
this.show = function(){
console.log("call person name:"+this.name+" age:"+this.age);
}
}
function student(name,age,grade){
person.call(this,name,age);
this.grade = grade;
}
student.prototype.showGrade = function(){
console.log("student call: grade:"+this.grade);
};
var stu = new student("zjw",24,1);
stu.show();
stu.showGrade();
//
function person(name,age){
this.name=name;
this.age=age;
this.show = function(){
console.log("call person name:"+this.name+" age:"+this.age);
}
}
function student(name,age,grade){
this.person=person;
this.person(name,age);
delete this.person;
this.grade=grade;
this.showGrade = function(){
console.log("student call: grade:"+this.grade);
}
}
var stu = new student("zjw",24,1);
stu.show();
stu.showGrade();
// ,
function person(name,age){
this.name=name;
this.age = age;
this.show1 = function(){
console.log("person show1 call: name:"+this.name+" age:"+this.age);
}
}
person.prototype.show2 = function(){
console.log("person show2 call: name:"+this.name+" age:"+this.age);
};
function student(name,age,grade){
person.call(this,name,age);// name age show1
var prop;
//
for (prop in person.prototype) {
var proto = this.constructor.prototype;
if (!proto[prop]) {
proto[prop] = person.prototype[prop];
}
proto[prop]["super"] = person.prototype;
}
this.grade=grade;
}
student.prototype.show = function(){
console.log("student show call: name:"+this.name+" age:"+this.age+" grade:"+this.grade);
};
var stu = new student("zjw",24,1);
stu.show1();
stu.show2();
stu.show();
방법 4:
function person(name,age,obj){
console.log("person call"+arguments.length);
this.name = name;
this.age = age;
this.obj = obj;
this.method1 = function(){
console.log("method1 call:"+this.name+"--"+this.age+"---"+this.obj);
}
}
person.prototype.method2 = function(){
console.log("method2 call:"+this.name+"---"+this.age+"---"+this.obj);
};
function student(name,age,grade,obj){
person.call(this,name,age,obj);
}
function inherits(curClass,parentClass){
curClass.prototype = new parentClass();
}
inherits(student,person);
var stu1 = new student("zjw",24,1,[1,2,3]);
stu1.method1();
stu1.method2();
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바 기초 - 클래스 구조 기 계승자바 에 서 는 상속, 하위 클래스 가 부모 클래스 에서 계승 할 수 있 는 도 메 인 과 방법 을 자주 사용 합 니 다.구조 기 에 관 한 구조 기 는 일반적인 계승 과 크게 다르다.다음 코드 를 예 로 들 면: ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.