JavaScript---Temporary builder f ()

Source: Internet
Author: User

This article is also mentioned before, is purely boring, MO want to take offense. ~ (~ ̄▽ ̄) ~ ~ (~ ̄▽ ̄) ~ ~ (~ ̄▽ ̄) ~ ~ (~ ̄▽ ̄) ~ ~ (~ ̄▽ ̄) ~

As mentioned above, if all the prototype properties point to an identical object, the parent object is affected by the child object, which is not the result we want.

To solve this problem, we need to find a third party, to destroy the ambiguous relationship between them, then we can use an empty function F (), and set her prototype to our parent constructor, and then we can be new F () to create objects that do not contain the parent object properties, That is, the parent property I can no longer set, but also inherit all the properties of the parent object. Although a bit around, see a small example to understand:

functionHer () {};her.prototype.name= ' Anna '; her.prototype.toString=function(){    return  This. Name;
functionHis () {};varF =function(){}; F.prototype=Her.prototype;his.prototype=NewF (); His.prototype.constructor=His;his.prototype.name= ' Jock '; His.prototype.sex= ' Men ';
functionChild (width, height) { This. width =width; This. Hieght =height; }varF =function() {}f.prototype=His.prototype;child.prototype=NewF (); Child.prototype.constructor=Child;child.prototype.name= ' Alen ';

In this way, we can keep the prototype chain, (sometimes two people still need a third person to adjust the life, otherwise it will not be boring?) )

My._proto_ = = = Child.prototype;  // trueMy._proto_.constructor = = = Child;  // true // trueMy._proto_._proto_._proto_.constructor = = = her;  // true

The properties of the parent object are not overwritten by the properties of the object.

JavaScript---Temporary builder f ()

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.