Javascript prototype (2)

Source: Internet
Author: User

The prototype of JavaScript has been discussed in this article. For beginners of JavaScript, prototype is a very advanced topic, but it is not all about it.

I cannot explain it all. It means that it is easy to understand prototype. However, it is really difficult to fully understand prototype.

Today I will start from the basics of prototype. In the previous article, I talked about the prototype mode. In fact, this is also the meaning of the original type in JavaScript. The prototype attribute of an object in Javascript is interpreted as returning a reference to an object type prototype. This is a confusing explanation. It specifies an object to be copied.

Text is not as goodCodeIn the code above, the simplest is that any class inherits from the object class:

Function ()

{}

A. Prototype = new object ();

In fact, this is equivalent to copying the attributes and methods of the object to object A as a prototype of object A. It is the same as the prototype!

Well, I have probably understood the basic usage of prototype. Let's take a look at the usefulness of prototype.

    1. The simplest usage is to dynamically extend the methods and attributes of a class.

      Function people ()

      {

      This. Jump = function (){

      Alert ("I can jump ");

      }

      }

      Now we need to expand the method:

      People. Prototype. Run = function (){

      Alert ("I can run, too ");

      }

      Good. test:

      VaR P = new people ();

      P. Jump ();

      P. Run ();

       

       

      Next, let's take a look at the types of Javascript methods. I personally divide Javascript into three methods:

      <1> Class Method

      <2> Object Method

      <3> Prototype Method

       

      Let's look at the code first and then talk about the difference:

       

Function people (name)

{

This. Name = Name;

// Object Method

This. Introduce = function (){

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

}

}

// Class Method

People. Run = function (){

Alert ("I can run ");

}

// Prototype Method

People. Prototype. introducechinese = function (){

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

}

Test:

VaR p1 = new people ("windking ");

P1.introduce ();

People. Run ();

P1.introducechinese ();

Summary:

name

location

Format

class method

out-of-class

class name. method Name

Object method

class

This. method Name

Prototype Method

Out-of-class

Class Name. Prototype. Method Name

 

We use C # To illustrate these three methods:

The class method is actually the static method we are talking about:

For example, public static void run (){}

The object method is actually an instance method.

Public void introduce (){}

The prototype method is different. Since the method cannot be dynamically added to an object in C #, the prototype method does not exist in C. The prototype method is between the static method of C # And the instance method. It is called through an object, but the stored memory is similar to the static method, that is, all instance objects share the same copy.

(PS: Define the method as a prototype method whenever possible. The prototype method avoids the construction of attributes or methods during each call to the constructor, which saves space and time.)

    1. P1.introducechinese (); create an object.

      Remember my previous articleArticleIs there a replication mechanism? Why do everyone like batch production? Efficiency is the main factor. The same is true for light replication. Why do we need light replication because of its high efficiency. Prototype is also responsible for JavaScript applications in prototype mode. Using prototype to create objects is much faster than other methods.

      Sample Code:

      Function people (name, age)

      {

      This. Name = Name;

      This. Age = age;

      }

      VaR p1 = new people ("Xuan", 22 );

      VaR girls = [];

      VaR girlprototype = function (){};

      Girlprototype. Prototype = p1;

      For (VAR I = 0; I <100000; I ++)

      {

      Girls [I] = new girlprototype ();

      }

    2. Play with inheritance:

      The biggest application of prototype is actually play-to-inheritance. This is not discussed here. Please join another article: javascript play-to-inheritance (II).

 

Well, the basic application is complete. Let's talk about prototype's two sides: Angel and devil.

Prototype is an angel because of the above applications, JavaScript has increased a lot of flexibility, especially prototype inheritance, which is also the most inherited method of JavaScript.

He is the devil because of the following aspects:

    1. Defects of prototype inheritance. For more information, see JavaScript transfer inheritance (2).
    2. The prototype is actually equivalent to the shortest replication in the prototype mode.

Too flexible. Why is it too flexible? In fact, this is my first point. The flexibility of dynamically adding attributes and methods is certainly increased. However, we discuss a situation where 100 people develop a javascript project at the same time. Many inexperienced people fall in love with prototype. Is it still Object-oriented for a person to add a method to this class?Of course, this is my personal opinion. If you have any objection, please feel free to discuss it with me.

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.