JavaScript's prototype Object

Source: Internet
Author: User

Brief prototype:

In JS, each constructor has a prototype attribute prototype, because the value of this property is usually an object, also known as a prototype Object! You do not need to explicitly define the prototype object, because each constructor will have a prototype property, and typically in this prototype object, it will contain a constructor for the constructor property that points to the prototype object:

function A () {alert (' Hello ');} var B = new A (); Console.log (b.constructor);//returns the Literal function a () {alert (' Hello ')};

In the above code you will find that first you do not define the prototype of function A, and constructor, but when used, the constructor property of the instance object B of constructor a correctly points to constructor A;

We all know that JS is a prototype based on the language, the prototype here refers to the object's prototype properties! From this can be seen, in JS, the prototype is used to achieve inheritance! So why is it possible to inherit from an object's prototype? This is because in JS, all instance objects that are generated using the same constructor share the prototype object of the constructor, for example:

Defines the constructor function user (name, age) {this.name = name; This.age = age;} The prototype method that defines the constructor user User.prototype.getName = function () {alert (this.name);} User.prototype.getAge = function () {alert (this.age);} Generate instance var UserA = new User (' A ', ' ' n '), var UserB = new User (' B ', ' ') '; Usera.getname ();//ausera.getage ();// 22userb.getname ();//buserb.getage ();//30

As can be seen from the above code, in the constructor we do not define the method to get the name and get the age getname and getage; they are defined in the prototype object. But we have two instance objects of the user constructor in the bottom code UserA and UserB can use the method in the constructor's prototype Object! This is what it says: All instance objects generated using the same constructor share the prototype object of the constructor!

The constructor property in prototype:

The above simply describes the constructor property in prototype, which is the property that comes with the prototype object, and it is always the constructor of the prototype!

Defines the constructor function user (name, age) {this.name = name; This.age = age;} The prototype method that defines the constructor user User.prototype.getName = function () {alert (this.name);} User.prototype.getAge = function () {alert (this.age);} Generate instance var UserA = new User (' A ', ' ' n '), var UserB = new User (' B ', ' ') '; Console.log (usera.constructor = = = Userb.constructor) True

Of course, we can also dynamically set the value of the constructor property:

User.prototype.constructor = user;

It is not necessary to let the constructor attribute point to the constructor when we do not redefine the Overwrite prototype Object!

Overwrite prototype objects:

We briefly introduce the prototype object, and also give some examples of simple use, in the above example, we are to the constructor of the prototype object on the addition of methods or properties, there is a way we have to add once, this is not elegant enough, So is there a way to define these methods at once? The answer is certainly yes, this is what we are going to cover the prototype object, we first look at how to do, the following we rewrite the above example:

Defines the constructor function user (name, age) {this.name = name; This.age = age;}    The prototype method that defines the constructor user User.prototype = {constructor:user, getname:function () {alert (this.name);    }, Getage:function () {alert (this.age); }}

You can copy the above code to see if the effect is the same as before! I believe you should understand after reading the code, since prototype is an object, then of course we can redefine an object to overwrite it, but it is worth noting that in the prototype object, the default is to include the constructor attribute to the constructor, When we define an object to overwrite the prototype object, we explicitly define the constructor property to point to the constructor, otherwise the constructor property of the prototype object will be undefined!

Total Knot:

Speaking here, about the prototype object of some knowledge and basic usage, has been explained to everyone clearly, because JS is based on the prototype inheritance, so understand the prototype object related objects, for JS object-oriented programming is crucial! About the prototype part of the first we talked about here, and so on later, we will continue to talk about the prototype chain, object-oriented and JS in the inheritance and other knowledge!

Thank you for reading, this article by the ordinary Childe original release, reproduced please indicate the source, thank you!

JavaScript's prototype Object

Related Article

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.