This article mainly introduces the relevant information about the properties, methods, and differences of JavaScript access objects. If you need a friend, you can refer to the property as a variable to indicate the characteristics of an object, such as color, size, and weight. The method is a function that represents object operations, such as running, breathing, and jumping.
In JavaScript, the "." operator is usually used to access the attribute values of an object. Alternatively, you can use [] as an associated array to access the attributes of an object.
The attributes and methods of an object are collectively called members of an object.
Access Object Attributes
In JavaScript, you can use "." and "[]" to access object attributes.
1. Use "." To Access Object Attributes
Syntax:
ObjectName. propertyName
Here, objectName is the object name and propertyName is the attribute name.
2. Use "[]" to Access Object Attributes
Syntax:
ObjectName [propertyName]
Here, objectName is the object name and propertyName is the attribute name.
Method of accessing objects
In JavaScript, you can only use "." To access objects.
Syntax:
ObjectName. methodName ()
Here, objectName is the object name, And methodName () Is the function name.
[Example 5-1] Create a Person class:
Function Person () {this. name = "Zhang San"; // defines an attribute name this. sex = "male"; // defines an attribute sex this. age = 22; // defines an attribute age this. say = function () {// define a method say () return "Hi! Hello everyone, my name is "+ this. name +", and the gender is "+ this. sex +". this year "+ this. age +" years old! ";}} Var zhangsan = new Person (); alert (" name: "+ zhangsan. name); // use ". to access object attributes alert ("Gender:" + zhangsan. sex); alert ("age:" + zhangsan ["age"]); // use "[]" to access the alert (zhangsan. say); // use ". "To access object Methods
PS: differences between "." and "[]" for Object Access attributes
In JavaScript, the "." operator is usually used to access the attribute values of an object. Alternatively, you can use [] as an associated array to access the attributes of an object. But what are the differences between the two methods?
For example, read the property value of an object:
Object. property
Object ['properties']
Both methods can achieve attribute access.
1. Syntax differences
The property name of an object in dot notation is an identifier, while the property name of the latter is a string.
2. Differences in flexibility
In JavaScript programming, you can create any number of attributes for an object. However, when you use the "." operator to access an object's attributes, the attribute name is represented by an identifier. In JavaScript programs, identifiers must be input word by word. They are not a data type and therefore cannot be operated by the program. That is to say, the identifier is static and must be hard-coded in the program.
When an object's attributes are accessed using array [] notation, the attribute names are represented by strings. Strings are a type of JavaScript data, so they can be operated and created in the program running.
3. Performance differences
Array [] notation is used to run expressions when accessing attribute values. The dot notation directly accesses the attribute values. Theoretically, the execution efficiency is higher than the array representation. Performance can be ignored.
In some scenarios, attribute values must be dynamically accessed using array notation, which cannot be achieved by dot notation.
In general, these two methods are slightly different and have corresponding application scenarios. Point notation is generally used as a static object to access attributes. Array representation is useful for dynamic access to attributes.