Schema parsing in JavaScript-prototype mode

Source: Internet
Author: User

Understanding the prototype pattern begins with understanding Prototyoe (the word translation prototype) attribute, as described inthe JavaScript Advanced Programming book--Each function we create has a prototype attribute, which is a pointer to an object, The purpose of this object is to include properties and methods that can be shared by all instances of a particular type.

As I said in the previous constructor pattern, we built a person function and then created a Person1 instance with the new person function, person2 instance, since each function has a prototype attribute, then I have a few questions to figure out:

    1. Which object does the prototype property of Person1 point to?
    2. The person object is also a function that we create, does it have a prototype property, and if so, where does its prototype attribute point?

The first answer, Person1 's prototype attribute, points to the person object (this should be understood)

So the second question looks a bit strange, where does the prototype attribute of the person object point? It can be customized by us, not an instance. The answer is that the prototype property of the person object points to the person object (if I don't understand it correctly)

This sounds awkward, the prototype object of the function is itself, but its true structure is this:

When we create a custom constructor (assumed to be person), the prototype property of the custom constructor points to the prototype object, but each prototype object has a constructor (translation constructor) attribute that points to our custom function. In this case, we should understand why I say that the prototype property of the person object points to itself. Let me give you an example of vivid image (for reference only). Person is a man, his left hand is called prototype, one day he wanted to find his prototype object, he put his left hand out to find Ah, and then caught a matching prototype object's right hand (constructor), he along the right hand of the prototype object to look up to the face of the prototype object, hey, this is not me? Person1 one day also want to find prototype object, he also stretched out his left hand (prototype) to find Ah, caught a matching prototype object's right hand, he also along the right hand of the prototype object to look up, gee, this is not person!

Haha, hope to make everyone laugh, then I have a question

What is the use of 3.prototype attributes?

functionPerson () {}person.prototype.name= "Guoshiwei"; Person.prototype.age=23; Person.prototype.sayName=function() {alert ( This. Name)};varperson1=NewPerson ();p erson1.sayname (); //"Guoshiwei"varPerson2 =NewPerson ();p erson2.sayname ()//"Guoshiwei"Alert (Person1.sayname= = Person2.sayname);//true

When we need a property or method that all instances need, we can add it to the prototype object, and all instances can share the properties or methods, and we can see that Person1 and Person2 refer to the same prototype object, in other words, as long as the new person () Created instance objects, they all refer to the same prototype object

There are some questions

Person.prototype.name= "Guoshiwei"; Person.prototype.age=23; var person1=New person ();p erson1.name= "Bushiguoshiwei"//   " Bushiguoshiwei "alert (person1.age)   //Delete// "Guoshiwei"

With this code, we can understand how object prototypes and instance objects work, and when we invoke the properties and methods in the instance, we first look at the properties and methods of the instance object itself, and if there is one, call it, if not, Let's go to the prototype object of this instance object to find these properties or methods, if there is, call, if there is no error (Error or display undefined ah? I'm not too sure.) If you do not know the meaning of this upward query, you can get to know the prototype chain,"JavaScript advanced Programming ," chapter fourth , this chapter is very important, the prototype chain also involves the closure of the problem.

I don't know if you've ever thought about it. Can you modify the properties or methods of a prototype object through an instance object?

        function Person () {        }        varnew person ();         = "Guoshiwei";         var New Person   ();        alert (person2.name);      // error, Person1.prototype is undefined (Firefox)

Is the instance object only referencing the prototype object, and only the constructor can be added or deleted (Can it be deleted?). Or is the constructor attribute in the prototype object pointing to who can manipulate the prototype object?

Overall: The constructor pattern can create an instance that can be identified, and the instance can have properties or methods in the constructor, and the prototype schema can add properties or methods that need to be shared to the prototype object, reducing the duplication of methods in the instance, so Constructor mode and prototype mode are the most common methods used when mixing!

Schema parsing in JavaScript-prototype mode

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.