Javascript: Prototype and inheritance (Part 2)

Source: Internet
Author: User

① Read and write inherited attributes

Each class has a prototype object with a set of attributes. However, a class has many potential instances, and each instance inherits these prototype attributes. Since a prototype property can be inherited by many objects, JavaScript must perform a basic asymmetry when reading and writing attribute values. When reading the P attribute of object o, JavaScript first checks whether O has an attribute named P. If not, it then checks whether O's prototype object has a property named P. This allows prototype-based inheritance to work.

On the other hand, JavaScript does not use prototype objects when writing a property value. To explore the cause, consider what will happen if it does so. Assume that you want to set the value of the property O. P, and the object o does not have a property named P. It is further assumed that JavaScript continues and finds the property P in the prototype object of O, and allows you to set the property of this prototype. Now, the P value of the entire class object has been changed, and this is not the intention.Therefore, property inheritance only occurs when the attribute value is read, but does not occur when the attribute value is written.If the P attribute of an object o is set, and the P attribute is inherited by O from its prototype, in this case, a new P attribute is directly created in O. Since o has its own property named P, it no longer inherits the value of P from its prototype. When reading the value of P, JavaScript first looks at the properties of O. Since P is defined in O, it does not need to look up the prototype object, the P attribute value defined in the prototype is not found. We sometimes say that the property P of O overwrites or hides the property P in the prototype object. Prototype inheritance may be a confusing topic and explains the concepts we discuss.

An error occurred while uploading the image.

BecauseA prototype attribute is a property shared by all objects of a class.So it makes sense to use them to define the same attributes for all objects in the class. This makes the prototype an ideal tool for defining methods. Other attributes with unchanged values (such as mathematical constants) are also suitable for defining with prototype attributes. If a class defines an attribute and often uses a default value, you can define this attribute and its default value in a prototype object. For a few objects that do not want to use the default value, you can create a private non-shared copy of the property and define them as their own non-default values.

② Extended built-in types

Not only do user-defined classes have original objects. Built-in classes such as string and date also have original type objects and can be assigned values for them. For example, the following code defines a method available in all string objects:

String.prototype.endsWith=function(c){return (c==this.charAt(this.length-1))}

After the new endswith () method is defined in the string prototype object, you can use it as follows:

var message="hello world";message.endsWith('h');  //falsemessage.endsWith('d');  //true

It is opposed to using your own methods to extend the built-in types.A good reason is that, in essence, a custom version of the core javascript API is created. If the Code contains methods that people have never heard of, any other programmer who must read and maintain the code may find the code too confusing. Unless you create an underlying JavaScript frame and expect many other programmers to use this frame, it is best to keep the original prototype objects away.

Note that attributes cannot be added for object. prototype. Any added attributes and methods can be enumerated using a for/in loop. adding them to object. prototype will make them visible in each JavaScript Object. An empty object {} should not have enumeration attributes. Any content added to object. Prototype becomes an enumerative attribute of this empty object. Using an object as an associated array may cause problems.

The extended built-in object type technology shown here only works for local objects of the core JavaScript. When Javascript is embedded in other environments, such as a web browser or a Java application, it accesses another host object, such as the object representing the content of the Web browser document. Generally, these host objects do not have constructors or prototype objects, and they cannot be extended.

In one case, it is safe and useful to extend the prototype of a built-in local class, that is, when an old or incompatible JavaScript implementation lacks a standard method, add this standard method to the prototype.

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.