js reuse code function
7884 단어 function
//Classic 1
function Parent(name) {
this.name = name || 'Adam';
}
Parent.prototype.say = function () {
return this.name;
};
function Child(name) {
}
function inherit(C, P) {
C.prototype = new P();
C.prototype.constructor = C;
}
inherit(Child, Parent);
var kid = new Child();
kid.name = 'Rita';
kid.say();
//One drawback of this pattern is that you inherit both own properties added to this and
//prototype properties. Most of the time you don’t want the own properties, because
//they are likely to be specific to one instance and not reusable.
//Classic 2 borrowed constructor pattern
function Article() {
this.tags = ['js', 'css'];
}
Article.prototype.GetTag = function () {
return this.tags[0];
};
var article = new Article();
function BlogPost() {
}
BlogPost.prototype = article;
var blog = new BlogPost();
function StaticPage() {
Article.call(this);
}
var page = new StaticPage();
alert(article.hasOwnProperty('tags')); // true
alert(blog.hasOwnProperty('tags')); // false
alert(page.hasOwnProperty('tags')); //true
blog.GetTag();
page.GetTag(); //Error
/*This way you can only inherit properties added to this inside the parent constructor.
You don’t inherit members that were added to the prototype.
Using the borrowed constructor pattern, the children objects get copies of the inherited
members, unlike the classical #1 pattern where they only get references.*/
/* 3 Multiple Inheritance by Borrowing Constructors*/
function Cat() {
this.legs = 4;
this.say = function () {
return "meaowww";
}
}
function Bird() {
this.wings = 2;
this.fly = true;
}
function CatWings() {
Cat.apply(this);
Bird.apply(this);
}
var jane = new CatWings();
console.dir(jane);
/*The drawback of this pattern is obviously that nothing from the prototype gets inherited
and, as mentioned before, the prototype is the place to add reusable methods and
properties, which will not be re-created for every instance.
A benefit is that you get true copies of the parent’s own members, and there’s no risk
that a child can accidentally overwrite a parent’s property.*/
/*4 Rent and Set Prototype*/
function Parent(name) {
this.name = name || 'Adam';
}
Parent.prototype.say = function () {
return this.name;
};
// child constructor
function Child(name) {
Parent.apply(this, arguments);
}
Child.prototype = new Parent();
var kid = new Child("Patrick");
kid.name; // "Patrick"
kid.say(); // "Patrick"
delete kid.name;
kid.say(); // "Adam
/*A drawback is that the parent constructor is called twice, so it could be inefficient. At
the end, the own properties (such as name in our case) get inherited twice.*/
/*5 Share the prototype*/
function inherit(C, P) {
C.prototype = P.prototype;
}
/*that is a drawback because if one child or grandchild
somewhere down the inheritance chain modifies the prototype, it affects all parents
and grandparents.*/
/*6 A Temporary Constructor*/
function inherit(C, P) {
var F = function () { };
F.prototype = P.prototype;
C.prototype = new F();
C.uber = P.prototype;
C.prototype.constructor = C;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
콜백 함수를 Angular 하위 구성 요소에 전달이 예제는 구성 요소에 함수를 전달하는 것과 관련하여 최근에 직면한 문제를 다룰 것입니다. 국가 목록을 제공하는 콤보 상자 또는 테이블 구성 요소. 지금까지 모든 것이 구성 요소 자체에 캡슐화되었으며 백엔드에 대한 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.