JavaScript Summary-create an object

Source: Internet
Author: User

JavaScript Summary-create an object

The following three statements are used to create objects in JavaScript:

Var box = new Object (); or var box = Object (); Var box ={}; // literal Function Box () {}; // Constructor

Now we will try to create an object

Var man = new Object (); // create an instance of the Object: manman. name = 'Mr. q'; // name attribute man of the man object. sex = 30; // nameman Of the man object. work = function () {return this. name + 'is working. ';}; alert (man instanceof Object); // true is the instance of the Object alert (man. work (); // Mr. Q is working.

What if I want to create an object similar to the above?

Solution 1: direct assignment; disadvantage: overwrite the original object

Var woman = man; woman. name = 'Ms. l'; // name attribute woman of the man object. sex = 21; // namewoman Of the man object. work = function () {return this. name + 'is working. ';}; alert (woman. name); // Ms. L overwrites the name attribute of the man object

Solution 2: instantiate an object in the same way. disadvantage: a large number of repeated codes are generated.

Var woman = new Object (); // create an instance of the Object: manwoman. name = 'Ms. l'; // name attribute woman of the man object. sex = 21; // namewoman Of the man object. work = function () {return this. name + 'is working. ';}; alert (woman. work (); // Mr. Q is working.

Optimization 1: factory mode: solves the problem of repeated code generated by instantiating a large number of similar objects

function createobject(name,sex){var obj=new Object();obj.name=name;obj.sex=sex;obj.work=function(){return this.name+' is working';};return obj;}var man=createobject('Mr.Q',30);var woman=createobject('Ms.L',21);alert(man.work());alert(woman.work());

Disadvantages: it is a factory model! For batch production, all products are similar. Objects cannot be distinguished. They are all instances of Object objects.
Optimization 2: constructor solves repeated instantiation and object recognition problems

Function Person (name, sex) {// human this. name = name; this. sex = sex; this. work = function () {return this. name + 'is working';} var man = new Person ('Mr. q', 'male'); var woman = new Person ('Ms. l', 'female '); alert (man instanceof Object); alert (man instanceof Person); function ET (name, sex) {// this. name = name; this. sex = sex; this. work = function () {return this. name + 'is working';} var marsman = new ET ('Mr. q', 'male'); alert (marsman instanceof ET); // truealert (marsman instanceof Person); // false mrasman is an instance of the ET object, not an instance of the Person object.

Summary: In this article, we briefly introduce three methods for creating an Object: Object, literal, and constructor. When many similar objects are created, the factory model is a good choice, but because of batch standardized production, products lack personality, we need to use constructors to distinguish different objects. However, when we use constructors to differentiate different objects, we still cannot avoid the issue of duplicate code generation, just as the Person and ET mentioned above have different constructors except for their names, so how can this be solved? I think prototype and inheritance are indispensable and will be introduced in subsequent articles.

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.