Deep analysis of Javascript:object type _ basics

Source: Internet
Author: User

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

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

var person = 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. constructor function mode

Use the new operator followed by the object constructor.

var p = new obejct ();
P.name = "The Rustling and the cold";
P.age = 18;

2. Object Literal method

A shorthand for defining objects to simplify the process of creating objects that contain a large number of properties. Example:

var p = {
  name: "The Rustle of the cold",
  age:18
};

Property names can also use strings when object literal syntax is used, for example:

var p = {
  "name": "The Rustle of the cold",
  "age":
  5:true
}

The example above creates an object that contains name, age, 53 attributes. The numeric property name here is automatically converted to a string.

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 = {};
P.name = "The Rustle is cold";
p.age = 18;

In general, access to the properties of an object is a dot notation, and in JavaScript you can access the object's properties using the square brackets notation. When you use the bracket 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 in functionality between these two ways of accessing. The advantage of the bracket syntax is that you can access the property through a variable:

var propname = "name";
Alert (P[propname]); "The rustling and the cold"

You can also use the square brackets notation if the property name contains keywords or reserved words that result in incorrect characters. For example:

p["the" "Name" = "The Rustle of the cold";

Property-Name contains a space that cannot be accessed by dot notation.

The above in-depth analysis of the Javascript:object type is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.