Home  >  Article  >  Web Front-end  >  What are JavaScript objects?

What are JavaScript objects?

青灯夜游
青灯夜游Original
2021-10-28 15:10:074352browse

In JavaScript, an object is a data collection with properties and methods. It is an unordered collection of related data in the form of "key: value" pairs; you need to use curly braces "{}" to define the object type. , the syntax is "{name1:value1,...,nameN:valueN}".

What are JavaScript objects?

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

In JavaScript, an object is a collection of data with properties and methods, and is a container of named values ​​called properties and methods. The data contained in an object can be accessed in two forms - properties and methods.

Object is a reference data type. A variable assigned a reference value is provided with a reference or pointer to that value. This reference or pointer points to the location in memory where the object is stored. Variables don't actually store values.

The object type is an unordered set composed of keys and values. To define an object type, you need to use curly braces { }. The syntax format is as follows:

{name1: value1, name2: value2, name3: value3, ..., nameN: valueN}

Where name1, name2, name3,..., nameN are the keys in the object, and value1, value2, value3,..., valueN are the corresponding values.

In JavaScript, the keys of object types are all string types, and the values ​​can be any data type, such as strings, arrays, functions, or other objects. To get a value in an object, you can use the form object name.key, as shown in the following example:

var person = {
    name: 'Bob',
    age: 20,
    tags: ['js', 'web', 'mobile'],
    city: 'Beijing',
    hasCar: true,
    zipcode: null,
    displayName: function() {
        document.write(this.name);
    }
};
console.log(person.name);       // 输出 Bob
console.log(person.age);        // 输出 20

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What are 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