JS creates an object instance in two ways (new operator and object literal)

Source: Internet
Author: User

Read JavaScript Advanced Programming (third Edition) Thoughts:

There are two ways to create an instance of an object, the first with the new operator followed by the object constructor, as follows:

var New Object ();       = "Zoumm";       = "Web Development";

The second is the use of object literal notation, which is intended to simplify the process of creating objects that create a large number of properties. As shown below:

var bb = {name:"Zoumm", Age:

Adding a comma after the last attribute will cause an error in IE7 and earlier China.

When using object literals, the property name can also use a string, as follows:

var bb = {    "name": "Zoumm",    "Age: ' 12,
"True"}//the numeric attribute name here is automatically replaced with a string

For object literals, it is recommended to only consider the readability of object property names. Object literals are also the preferred way to pass a large number of optional parameters to a function, such as:

  function aa (args) {          if(typeof args.name== "string" ) {                  console.log (args.name);             }           if(typeof args.age== "number" ) {              console.log (args.age);          }      }      AA ({          name:"Zoumm", age          :      ())      AA ({          name:"DJ"      }) 

JS creates an object instance in two ways (new operator and object literal)

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.