A brief analysis of JavaScript access object properties methods and differences

  • 2020-11-03 22:00:28
  • OfStack

Attribute is a variable used to represent the characteristics of an object, such as color, size, weight, etc. A method is a function that represents the operation of an object, such as running, breathing, jumping, and so on.

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

The properties and methods of an object are collectively called its members.

Access the properties of the object

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

1. Use ". "to access object properties

Grammar:

objectName.propertyName

Where objectName is the object name and propertyName is the attribute name.

2. Use "[]" to access object properties

Grammar:

objectName[propertyName]

Where objectName is the object name and propertyName is the attribute name.

Method to access an object

In JavaScript, only ". "can be used to access object methods.

Grammar:

objectName.methodName()

Where objectName is the object name and methodName() is the function name.

[Example 5-1] Create an Person class:


function Person() {
  this.name="  zhang 3 "; //  define 1 A property  name
  this.sex="  male  "; //  define 1 A property  sex
  this.age=22; //  define 1 A property  age
  this.say=function(){ //  define 1 A method of  say()
    return " A: hi! Hello, my name is  " + this.name + "  Gender is  " + this.sex + " This year,  " + this.age +" Years old! ";
  }
}
var zhangsan=new Person();
alert(" Name: "+zhangsan.name); //  The use of" . "To access object properties 
alert(" Gender: "+zhangsan.sex);
alert(" Age: "+zhangsan["age"]); //  The use of" [ ] "To access object properties 
alert(zhangsan.say);  //  The use of" . "To access object methods 

PS: A brief analysis of the differences between "." and "[]" methods of object access properties

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

For example, read the property attribute value in object:

object.property

object['property']

Either way, access to attributes can be achieved.

1. Differences in grammar

The property name of the dot representation object is an identifier, while the property name of the latter is a string.

2. Differences in flexibility

In the JavaScript program, you can create any number of attributes for an object. But when the ". "operator is used to access a property of an object, the property name is expressed as an identifier. In JavaScript, identifiers must be entered verbatim; they are not a data type, so the program cannot operate on them. That is, the identifier is static and must be hard-coded in the program.

When the array [] notation is used to access an object's property, the property name is represented as a string. Strings are one of the data types of JavaScript, so you can manipulate and create them while your program is running.

3. Performance differences

The array [] representation runs the expression when the property value is accessed. The point representation, which accesses property values directly, is theoretically more efficient than the array representation. The performance aspect can be ignored.

Some scenarios must use array notation to dynamically access property values, which point notation cannot do.

Generally speaking, there is little difference between the two methods, and both of them have corresponding usage scenarios. Dot notation 1 generally accesses properties when used as a static object. Array notation is useful for dynamically accessing properties.


Related articles: