Notes on javaScript object-oriented and prototype, javascript object-oriented

Source: Internet
Author: User

Notes on javaScript object-oriented and prototype, javascript object-oriented


JavaScript is a special language. ECMAScript does not have the class concept and is different from other object-oriented languages, its objects are also different from those in class-based languages. Strictly speaking, javascript objects are a group of values without specific sequence. Each attribute 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 a custom Object is to create an Object instance and then add attributes and methods to it, such:

Var box = new Object (); // create an Object box. name1 = 'lil'; // Add the property box. age = 100; // box. run = function () {return this. name1 + this. age // this indicates the object under the current scope


Insufficient: using the same interface to create many objects will produce a lot of repeated code.


Factory model:

The class cannot be created in ECMAScript, so developers come up with another method to encapsulate the details of object creation with a specific interface using a function, such:

   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',29,'Software Engineer');    var person2 = createPerson('Greg',27,'Doctor');    person1.sayName();    person2.sayName();    alert(typeof person1);    alert(typeof person2);

The creatPerson () function can construct a Person object containing all necessary information based on accepted parameters. This function can be used in a low-key manner for countless times, each time it returns an object of a method containing three attributes. Although the factory mode solves 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 person1 = new Person('lilei',29,'Software Engineer');    var person2 =new Person('Greg',27,'Doctor');    person1.sayName();    person2.sayName();    alert( person1 instanceof Object);     alert( person2 instanceof Person);

 

 

In this example, the Person () function replaces the createPerson () function. Note the differences with the createPerson () function:

No explicit object creation;

Attributes and methods are directly assigned to this object;

No return statement;

The first letter of the composition method is capitalized;

There are also constructor problems, that is, each method must be re-created on each instance,

Constructor mode and prototype mode

A common way to create a custom type is to combine the constructor mode and the prototype mode. The constructor mode is used to define instance attributes, while the prototype mode defines methods and shares attributes, so that each instance has its own copy of instance attributes, but at the same time, it shares the reference of the method of the other party,

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',29,'Software Engineer');    var person2 = new Person('Greg',27,'Doctor');    person1.friends.push("Van");    alert(person1.friends);    alert(person2.friends);    alert(person1.friends === person2.friends);    alert(person1.sayName === person2.sayName);

In this example, the instance attributes are defined in the constructor, while the consructor and sayName () attributes shared by all instances are defined in the prototype, when person1.friends is modified, it does not affect person2 ,. friends, because they reference different arrays.


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.