Home  >  Article  >  Web Front-end  >  What are the characteristics of javascript objects

What are the characteristics of javascript objects

青灯夜游
青灯夜游Original
2021-12-07 15:58:202820browse

Characteristics of JavaScript objects: 1. The last attribute in the "key-value pair" list must end with a comma; 2. The data of an object declared using const can be modified; 3. The attribute name can It is the "[value]" method; 4. The left side of the "in" operator must be the attribute name, the right side is the object name, and the returned value is a Boolean value.

What are the characteristics of javascript objects

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Nine characteristics of js objects

First: The last attribute in the "key-value pair" list must end with a comma

This comma has a fancy name: trailing comma (trailing)

The reason should be for standardization, or simply for beauty.

Second: The data of an object declared using const can be modified

The properties inside the object can be modified.

It is not possible to change the entire object.

Third: Multi-word attribute names to mess with

The key in the key-value pair, that is, if the name in name: "zhangsan" becomes "new name ".

Changing the name from one word to multiple words will cause some things to change.

  • Points can no longer be used. It used to be person.name, but now we cannot write person.new name.
  • You should write person["new name"]

When you encounter a multi-word name, remember three points:

  • Use brackets

  • Name in quotes

  • You can write strings directly in the brackets, or you can write variables, because strings can also be written in variables

Fourth: There is a weird way to name attributes, square brackets[]

 let name="apple"
 var o={
     [name]:5,
 }
 alert(o.apple);

Remember, in square brackets What is stored is not a certain, rigid fixed value, but a variable. Do you understand variables?

The kind that is flexible and changeable.

Fifth: Under special circumstances, the attribute value can be abbreviated

function makeUser(name, age) {
  return {
    name: name,
    age: age,
    // ……其他的属性
  };
}

let user = makeUser("John", 30);
alert(user.name); // John

It can be obtained by observation that the attribute name and variable name are the same.

At this time, you can change the writing method:

Before the change: name: name

After the change: name

What is the meaning? It is just for convenience. In a sense, it also increases the burden on beginners. Therefore, everything has two sides and nothing is wrong.

Sixth: The attribute name can be chosen casually

No need to worry about keywords not being used (Why do you have to use keywords? It hurts to be idle)

Remember one thing: __proto__ attributes. We cannot set it to a non-object value

Seventh: The role of "in"

"key" in object
  • The attribute name is in in the object.

The left side of in must be the attribute name, the right side is the object name, and the returned value is Boolean true or false.

The attribute name is usually a string, but it may also be a variable, and the variable is still a string.

So strings are still working.

Why in?

Because I am afraid that undefined will cause trouble.

Eighth: for...in loop

Grammar format:

 for (key in object) {
   // 对此对象属性中的每个键执行的代码
 }

Among them, except for the key on the left of in which is uncertain (can be replaced by other words), the structures of the other words are certain.

The side reflects that the important thing in this statement is "which object is to be traversed".

Ninth: The order of object attributes

One concept: integer attribute name

The attribute name is an integer string

Another A concept: Integer string

can be converted into integer string

"1", "2", etc. are integer strings.

Remember:

  • When the attribute name is not an integer string, the order when traversing the object to output data is in the order of creation
  • When the attribute name is an integer character Strings, in order from smallest to largest.

[Related recommendations: javascript learning tutorial]

The above is the detailed content of What are the characteristics of javascript objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is node expressNext article:what is node express