On the properties and methods of JavaScript access objects and their differences

Source: Internet
Author: User
Tags access properties

A property is a variable that represents the characteristics of an object, such as color, size, weight, and so on; A method is a function that represents an object's operation, such as running, breathing, jumping, and so on.

"." is commonly used in JavaScript. operator to access the value of an object's properties. or use [] as an associative array to access the properties of the object.

The properties and methods of an object are collectively referred to as members of the object.

Accessing the properties of an object

In JavaScript, you can use the. "and" [] "to access the properties of the object.

1. Use ". To access the object properties

Grammar:

Objectname.propertyname

Where ObjectName is the object name and PropertyName is the property name.

2. Use "[]" to access object properties

Grammar:

Objectname[propertyname]

Where ObjectName is the object name and PropertyName is the property name.

Methods for accessing objects

In JavaScript, you can only use ". "To access the object's methods.

Grammar:

Objectname.methodname ()

Where ObjectName is the object name and MethodName () is the function name.

"Example 5-1" creates a person class:

function person () {  this.name= "Zhang San"; Define a property name  this.sex= "male"; Define an attribute sex  this.age=22;//define an attribute age  this.say=function () {//define a method say ()    return "Hey! Hello everyone, my name is "+ THIS.name +", sex is "+ This.sex +", this year "+ This.age +" years old! ";  }} var zhangsan=new person (); alert ("Name:" +zhangsan.name); Use the "." To access the object Properties alert ("Gender:" +zhangsan.sex); alert ("Age:" +zhangsan["ages"]); Use "[]" To access object Properties alert (Zhangsan.say);  Use the "." To access Object methods

PS: An analysis of the "." Of object access properties and "[]" Method difference

"." is commonly used in JavaScript. operator to access the value of an object's properties. or use [] as an associative array to access the properties of the object. But what's the difference between these two ways?

For example, read the property attribute value in object:

Object.property

Object[' property ']

Both of these methods enable access to attributes.

1. Grammatical differences

The property name of the object with point notation is an identifier, and the latter is a string.

2. Differences in flexibility

In JavaScript authoring programs, you can create any number of properties for an object. But use the "." operator to access the properties of an object, the property name is represented by an identifier. In JavaScript programs, identifiers must be entered literally, and they are not a data type, so the program cannot manipulate it. That is, identifiers are static and must be hardcoded in the program.

When using array [] notation to access the properties of an object, the property name is represented by a string. Strings are a data type of JavaScript, so you can manipulate and create them in a program run.

3. Differences in Performance

The array [] notation is run as an expression when accessing the value of the property. The point notation is the direct access to the attribute value, which is theoretically higher than the array notation. Performance aspects can actually be ignored.

Some scenarios must use the array notation to dynamically access the attribute values, which is not possible with dot notation.

In general, these two methods are not very different, there are corresponding usage scenarios. Point notation is generally used as a static object to access properties. Array notation is useful for dynamically accessing properties.

On the properties and methods of JavaScript access objects and their differences

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.