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;
}

좋은 웹페이지 즐겨찾기