JavaScript Summary--creating objects

Source: Internet
Author: User

The main purpose of creating objects in JavaScript is to use the following three statements :

    1. var box=new Object (); or var box=object ();
    2. var box={};//literal
    3. function Box () {};//constructor

Now, let's kind to create an object

Var man=new object ()//Create an instance of the object: Manman.name= ' mr.q ';//man object's Name property Man.sex=30;//man Object Nameman.work=function () {return this.name+ ' is working. ';}; Alert (man instanceof object),//true is an instance of object, alert (Man.work ());//mr.q is working.

What if you want to create objects that are similar to the objects above?

Scheme one : direct assignment; disadvantage: Overwrite original object

var woman=man;woman.name= ' MS.L ';//man object's Name property Woman.sex=21;//man object's Namewoman.work=function () {return this.name+ ' is working. ';}; alert (woman.name);//ms.l overrides the Name property of the man object

Scenario Two : Instantiate an object in the same way; disadvantage: Generate a lot of duplicate code

Var woman=new object ()//Create an instance of object: Manwoman.name= ' MS.L ';//man object's Name property Woman.sex=21;//man object namewoman.work= function () {return this.name+ ' is working. ';}; Alert (Woman.work ());//mr.q is working.

optimize one : Factory mode: Solve instantiating a large number of similar objects to produce duplicate code problems

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 ', $), var woman=createobject (' MS.L ', +); alert (Man.work ()); alert (Woman.work ());

cons : Because it's a factory model! Mass production, the product is a sample. Objects cannot be distinguished from each other, they are all instances of object objects.
optimization Two : constructors solve repetitive 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); person); function ET (name,sex) {//Alien 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 an ET object, not an instance of the person object

Summary : In this article we briefly describe the three ways to create an object: Object, literal, and constructor, and when creating many similar objects, Factory mode is a good choice, but because of the production of batch standardization, the product lacks individuality, So we're going to use constructors to distinguish between different objects. But when we use constructors to differentiate between different objects, we still avoid the problem of generating duplicate code, just like the person above and et they are not the same as the constructor name, the others are the same, so how can this be solved? I think that the use of prototypes and inheritance is unavoidable and will be covered in subsequent articles.

JavaScript Summary-Create object

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.