javascript--constructor Mode

Source: Internet
Author: User

var New Array ();

We created an object that is a group type, and array is a native constructor. Obviously, the constructor pattern is to let us know if we are going to create a person (), how can we make it like an array () to create an object instance from a new class, and this object instance is marked as the person type

 function   person (name, age, job) { Span style= "color: #0000ff;"    >this . Name=name;     this . Age=age;     this . Job=job;  this . Sayname = function   () {alert ( this  .name); };} var  person1= new  person ("Guoshiwei", 23, "Web"  var  person2 = new  person ("Nicholas", 29, " Software Engineer "); 

This code uses the constructor pattern, and when you create an instance from the constructor pattern, you must use the new operator, which is identified by the new operator as the person type (the greatest benefit of the constructor)

Attention:

The first letter of the person should be capitalized, because the constructor mode itself is also a function, in order to distinguish, the function of the construction mode of the first letter capital

The process of creating a new instance through new is as follows (after comment for personal understanding)

    1. Create a new object;//Open a space in memory object type objects all objects that are not yet defined are object objects
    2. Assigns the scope of the constructor to the new object (so this is the new object);//Copies a copy of the class or method in the person class into this space, at which point the object becomes the person type object constructor property points to person Indicate which of these properties and methods you are getting from
    3. Execute the code in the constructor (add a property to the new object);//Put your parameters into the properties or methods of this space
    4. Returns a new object;//Connect This object to the Person1

So far, the Person1 object instance of a person type is done, but it is noted that the object instance created by the new operator, the second step, I understand is that it is to copy the properties and methods in the person class to themselves, rather than referencing properties and methods from person. (Each property and method is its own)

Alert (Person1.sayname = = person2.sayname);   // false

This creates a kind of inconvenience (which I understand to be wasteful), which is to regenerate a sayname function every time an instance is created.

function Person (name, age, Job) {    this. name=name;      this. age= age;     this. job=job;     this. sayname=sayname;
}function  sayname () {    alert (this. name);} var person1=New person ("Guoshiwei", "page", "Web"); var person1=New person ("Nicholas", "Software Engineer");

You can create the Sayname function outside of the function so that both Person1 and Person2 refer to the Sayname () method through the Sayname object, but if you still need sayage, the Sayjob function is more troublesome, and once you need more methods, is not worth the candle, and reduces the encapsulation of the person function, so in order to solve this problem, there is a new mode- prototype mode

javascript--constructor Mode

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.