JS reuse code Function

Source: Internet
Author: User
Tags hasownproperty
//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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.