What are JavaScript objects?

青灯夜游
Release: 2021-10-28 15:10:07
Original
4293 people have browsed it

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}
Copy after login

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
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!