Javascript:object type

Source: Internet
Author: User
Tags access properties

In JavaScript, a reference type is a data structure used to organize data and functionality together .

An object is an instance of a particular reference type. How objects are created:

var New Object ();

The example above creates a new instance of the object reference type and then saves the instance in the variable person.

There are two ways to create objects that have constructors and object literals .

1. How to construct a function

Use the new operator followed by the object constructor.

var New  = "Rustle of the cold" = 18;

2. Object Literal method

A shorthand for an object definition to simplify the process of creating an object that contains a large number of attributes. Example:

var p = {    name:"rustling the Cold", age    :+};

In the case of object literal syntax, the property name can also use a string, for example:

var p = {    "name": "Rustling the Cold",    "age":    5:true} 

The example above creates an object that contains the name, age, and 53 properties. The numeric property names here are automatically converted to strings.

In addition, when you use object literal syntax, you can define an object that contains default properties and methods if you leave the curly braces blank. For example:

var p == "Rustle of the cold" = 18;

In general, the properties of Access objects use dot notation, and in JavaScript you can also use square brackets to access the properties of an object. When you use square brackets syntax, you should place the property you want to access in square brackets as a string, for example:

Alert (p["name"]); alert (p.name);

There is no difference between these two ways of accessing functionality. The advantage of square brackets syntax is that you can access properties by using variables:

var propname = "name"//  "rustling the Cold"

You can also use square bracket notation if the property name contains characters such as keywords or reserved words that can cause errors. For example:

p["First Name"] = "rustling the cold";

The property first name contains a space that cannot be accessed by dot notation.

Javascript:object type

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.