JS function prototype attribute learning (i.)

Source: Internet
Author: User

The definition and usage of the prototype property on W3school are as follows: It gives you the ability to add properties and methods to objects. Then look at the example given on W3school, such as:

Looking carefully, the original function is to extend the properties and methods of some objects, and I have written a few more lines of code to test this example, as follows:

var steve= new Empolyee ("Steve Jobs", "Enterpriser", 1977);

Here I look specifically at the attributes that Steve has in this entity, found a salary attribute, which is not defined when creating a new entity, visible because of the role of prototype, employee this object has a new property salary, then, Prototype in the end is the sacred, it in the JS object in the end have how indispensable role?

Each object in JavaScript has the prototype property, and the prototype property of the object in JavaScript is interpreted as a reference to the object type prototype. There is no concept of class in JavaScript, but it is true that JavaScript can be overloaded, polymorphic, and inherited. These implementations can all be explained by reference and variable scopes in JavaScript, combined with prototype. The main idea of the prototype method is that there are now 1 class A, and I want to create a class B, which is a prototype and can be extended. We call the prototype of B A.

Given the feeling that class A is particularly like the inherited base class in Java, and the Class B inherits the entity subclass, the extension to Class B is like polymorphism, or the rewrite of an existing method, which is object-oriented with wood, which is the best gift that JavaScript gives us, although it does not directly show that it supports object-oriented, But secretly but silently support. There are different ways to look at JavaScript.

1. Class methods

var person = {

};

Person.say = function () {

Alert ("I can say language");

};

2. Object methods

function person (name) {

THIS.name = name;

this.interduceself= function () {

Alert ("My name is" + this.name);

};

};

3. Prototype method

Person.prototype.interduceSelfChinses = function () {

Alert ("My name is" + this.name);

};

Person.say ();

var person1 = new Person ("xiaoming");

Person1.interduceself (); The result is the My name is Xiaoming

Person1.interduceselfchinses (); It turns out my name is xiaoming.

var person2 = new Person ("Xiaozhang");

Person2.interduceself (); The result is the My name is Xiaozhang

Person2.interduceselfchinese (); It turns out my name is Xiaozhang.

From this we can see that as soon as the method in the prototype property of an object is added, this method becomes the common method of this object, regardless of what entity you create later, has this common method and exists in its _proto_ attribute.

As you can see, there is no new way for all people to Interduceselfchinese, there is this public method automatically, because when an object is created, the constructor assigns its property prototype to the inner property of the new object _proto_, which _ The Proto_ property is used by this object to find its prototype property, including its properties, methods, are found, which is called when called. So there is a benefit, you do not need to construct an object every time you create and initialize this function, inheritance is implemented, but strictly speaking, JS does not provide the concept of inheritance, it can be said that the new entity is a copy of the original object, and this copy is very reflecting, you can re-write the properties and methods inside , you can also add the properties or methods you want, and take a look at the following example:

function Fatherclass () {

This.class = "Father";

this.setauthority = function () {

This.authority = "Controller";

};

};

var child = new function () {};

Child.prototype = new Fatherclass ();

Child.prototype.class = "Child";

child.prototype.setAuthority = function () {

this.authority = "controlled";

};

Child.say = function () {//New method, add to new object directly

Alert ("You can ' t controll me");

};

alert (Child.class); The result is child

alert (child.authority); The result is controlled

Child.say (); Results for you can ' t controll me

As can be seen from the above, functional programming is also profound, this is the art of a programming language. At the same time, the properties or methods of the custom type cannot be changed externally by prototype. As you can see, the properties and methods of the call are still the result of the original definition. That is, when the prototype method and the object method invoke the same properties and functions, the properties and functions inside the object's methods are executed. Let's look at the following example:

function Fatherclass () {

This.class = "Father";

this.setauthority = function () {

This.authority = "Controller";

};

};

FatherClass.prototype.class = "Child";

FatherClass.prototype.setAuthority = function () {

this.authority = "controlled";

};

alert (Fatherclass.class); The result is still father

alert (fatherclass.authority); The result is still controller

That is, you cannot modify the object itself and any properties or methods directly through the prototype property, or create a new entity that copies it and modify it, or add a new property or method.

To understand these 2 things again,

1, A.prototype = new B ();

2, a.prototype = B.prototype;

First Look at a.prototype = new B (), understand that prototype should not confuse it with inheritance. A's prototype is an example of B, and it's understandable that a will clone the methods and properties in B all over again. A can use the methods and properties of B. The emphasis here is on cloning, not inheritance. This can happen: the prototype of A is an instance of B, and the prototype of B is also an instance of a.

function BaseClass () {

This.showmsg = function () {

Alert ("Baseclass::showmsg");

}

} ;

function Extendclass () {};

Extendclass.prototype = new BaseClass ();

var instance = new Extendclass ();

Instance.showmsg (); The result is baseclass:showmsg.

Here we use an example of BaseClass to prototype, to clone the Extendclass also contains showmsg this method. Extendclass.prototype = new BaseClass () can be read as: Extendclass was created as an instance of BaseClass as a prototype clone. But what happens when you call this function when there is a function in Extendclass with the same name as ShowMsg? Look again at this example:

function BaseClass () {

This.showmsg = function () {

Alert ("baseclass:showmsg base");

}

};

function Extendclass () {

This.showmsg = function () {

Alert ("extendclass:showmsg extend");

}

};

Extendclass.prototype = new BaseClass ();

var Extenda = new Extendclass ();

Extenda.showmsg (); The result is extendclass:showmsg extend

As can be seen from here, the function will go to the function of the main body to find, if found to run, will not find the prototype to find the function. Or it can be understood that prototype does not clone a function with the same name. What if you want to invoke the same name method in the original class directly? You can use the call () method to do this, and then write the following code:

Extendclass.prototype = new BaseClass ();

var instance = new Extendclass ();

var baseinstance = new BaseClass ();

Baseinstance.showMsg.call (instance); The result is baseclass::showmsg base

Here's Baseinstance.showMsg.call (instance); read as "invoke instance as Baseinstance, invoke its object method ShowMsg"

See A.prototype = B.prototype,a's prototype is B's prototype, you can understand that a will B's prototype in the method and properties are cloned all over again. A can use the prototype method and properties of B. The emphasis here is on cloning, not inheritance. Take a look at the following example:

function Blog (url,name) {

This.url = URL;

THIS.name = name;

};

Blog.prototype.jumpurl = ";

Blog.prototype.jump = function () {

window.location = This.jumpurl;

};

var blog1 = New Blog (' Dearxiangxiao ', ' https://home.cnblogs.com/u/xiangxiao/');

var blog2 = New Blog (' The Sea of Shihiri ', ' http://www.cnblogs.com/weixuqin/');

var webblog = function () {};

Webblog.prototype = Blog.prototype;

var mywebblog = new Webblog ();

As can be seen from the above implementation results, Mywebblog's two attributes jump and Jumpurl refer to Blog.prototype.jumpurl and Blog.prototype.jump respectively, so the prototype of an object can also be used as a reference to the prototype of another object.

Conclusion: Feel JS inside the class and entity object declaration of the method is not much attention, class methods can also be directly used, you know, in Java can be an abstract method to allow such invocation.

JS function prototype attribute learning (i.)

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.