About JavaScript Object-oriented and prototype notes

Source: Internet
Author: User


JavaScript is a special language, and there is no concept of class in ECMAScript. There are some differences with other object-oriented languages. Its objects are also different from those in class-based languages, and strictly speaking, JavaScript objects are a set of values that do not have a particular order, and each property or method of an object has a name. Each name is mapped to a value. Each object is created based on a reference type.


The simplest way to create your own definition object is to create an instance of object. Then add properties and methods to it, such as:

var box = new Object ();        Create Object box.name1 = ' Lee ';                    Add attribute box.age = +;                        Box.run = function () {    return this.name1 + this.age       //this represents the object under the current scope


Insufficient: Creates very many objects using the same interface. will produce very much repetitive code.


Factory mode:

Unable to create a class in ECMAScript, the developer came up with a second approach, using a function to encapsulate the details of creating objects with a specific interface, such as:

   function Createperson (name,age,job) {        var o = new Object ();        O.name = name;        O.age = age;        O.job = job;        O.sayname = function () {            alert (this.name);        }        return o;    }    var person1 = Createperson (' Lilei ', ' software Engineer ');    var person2 = Createperson (' Greg ', ' Doctor ');    Person1.sayname ();    Person2.sayname ();    Alert (typeof Person1);    Alert (typeof Person2);

The function Creatperson () can construct a person object that includes all the necessary information according to the accepted parameters. You can use this function many times in low-key. And each time it returns an object that contains a method of three attributes.

Although the factory model overcomes the problem of creating multiple types of objects, it does not solve the problem of object recognition.

Constructor mode

function Person (name,age,job) {        this.name = name;        This.age = age;        This.job = job;        This.sayname = function () {            alert (this.name);        };    }    var = new person (' Lilei ', Person1, ' software Engineer ');    var person2 =new person (' Greg ', ' Doctor ');    Person1.sayname ();    Person2.sayname ();    Alert (Person1 instanceof Object);     Alert (person2 instanceof person);

In this example. The person () function replaces the Createperson () function. Notice the difference with the Createperson () function at the same time:

Object not explicitly created;

The properties and methods are assigned directly to the This object;

no return statement;

The first letter of the constituent method is capitalized.

The problem with constructors is that each method is created again and again on each instance,

Combining the constructor pattern with the prototype pattern

The common way to create your own definition types is to combine the constructor pattern with the prototype pattern.

The constructor pattern is used to define instance properties, while the prototype schema uses the definition method and shared properties so that each instance will have its own copy of the instance properties. But at the same time they shared the reference of each other's methods.

function Person (name,age,job) {    this.name =name;    This.age = age;    This.friends = ["Shelpy", "Court"];    }    Person.prototype = {    Constructor:person,    sayname:function () {    alert (this.name)    }    }    var Person1 = new Person (' lilei ', +, ' software Engineer ');    var person2 = new Person (' Greg ', ' Doctor ');    Person1.friends.push ("Van");    alert (person1.friends);    alert (person2.friends);    Alert (person1.friends = = = Person2.friends);    Alert (Person1.sayname = = = Person2.sayname);

In this example. Instance properties are defined in the constructor. The properties Consructor and Method Sayname () shared by all instances are defined in the prototype. When the person1.friends is changed, it does not affect the person2,.friends. Because they refer to different arrays.


About JavaScript Object-oriented and prototype notes

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.