On the properties and methods of JavaScript access objects and the skills of distinguishing _javascript

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 the operation of an object, such as running, breathing, jumping, and so on.

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

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 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.

Ways to Access Objects

In JavaScript, you can only use the. "To access the method of the object.

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= "John"; Define a property name
  this.sex= "male"; Define a property sex
  this.age=22//Define a Property age
  this.say=function () {//define a method Say () return
    "Hey! Hello everyone, my name is "+ THIS.name +", the gender is "+ This.sex +", this year "+ This.age +" years old! ";
  }
}
var zhangsan=new person ();
Alert ("Name:" +zhangsan.name); Use the "." To access the object property
alert ("Sex:" +zhangsan.sex);
Alert ("Age:" "+zhangsan["]); Use [] To Access object property
alert (Zhangsan.say);  Use the "." To access the object method

PS: An analysis of the object access properties of "." differs from the [] method

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

For example, to read the property attribute value in object:

Object.property

Object[' property ']

Both of these methods enable access to the property.

1. Grammatical differences

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

2. Differences in flexibility

In a JavaScript writer, 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 typed verbatim, they are not a data type, and therefore the program cannot operate on them. In other words, the identifier is static and must be hard-coded in the program.

When you use the 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. Difference in performance

The array [] notation runs as an expression when accessing the property value. While the point representation is the direct Access attribute value, the efficiency of execution is higher than that of the array representation method. Performance can actually be overlooked.

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

In general, the two methods are not very different, there is a corresponding use of the scene. The point notation is generally used as a static object to access the property. Array notation is useful for dynamically accessing properties.

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.