js reuse code function

來源:互聯網
上載者:User
//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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.