5.1 Object Type

Source: Internet
Author: User

There are two ways to create an object instance

① using the new operator with the object constructor

var person = new Object ();p erson.name = "Tom";p eison.age = 25;

② literal: The feeling of being encapsulated

var person = {    name: ' Tom ',  //' name ' = ' Tom '    age:25       //' age ' = 25};

Property names can also use strings.


The literal passes a large number of optional parameters:

function DisplayInfo (args) {        var output = "";        if (typeof Args.name = = "string") {            output = "Name:" + args.name + "\ n";        }        if (typeof args.age = = "Number") {            output = "Age:" + args.age + "\ n";        }        alert (output);    }    DisplayInfo ({    //literal parameter        name: "Tom",        age:25    });

Access Object properties can be expressed in dot notation and square brackets  

Alert (person["name"]);  Square brackets alert (person.name);     Point//variable to access the property var propertyname = "Name"; alert (Person[propertyname]);

You can also use square brackets if the property name contains a character that causes a syntax error, or if the property name uses a keyword or reserved word.



5.1 Object Type

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.