JavaScript Objects (details)

JavaScript Objects (details)

JavaScript Objects

1. Definition

An object is a reference data type and a container for storing complex data types. It is a collection of multiple attributes (data) and methods (functions).

It allows dynamic addition and removal of attributes

2. Classification of objects

(1) Built-in objects

Objects defined in the ES standard can be used in any ES implementation

For example: math string number boolean object...

(2) Host object

Objects provided by the js runtime environment, currently mainly provided by the browser

For example, BOM DOM

(3) Custom objects

Objects created by developers themselves

3. Define the object

Object literals

Using object literals, you can directly specify the properties in the object when creating the object

Syntax: {attribute name:attribute value, attribute name:attribute value...}

The property names of object literals can be quoted or not, but it is recommended not to quote them.

If you want to use some special names, you must put them in quotes.

Use '{ }' as the boundary, separate attributes with ',', and separate attributes and attribute values ​​with ':'

var obj={};

Constructor creates an object

The function called with the new keyword is the constructor

Constructor is a function used to create objects

When you use typeof to check an object, it returns object.

var obj = new Object();
console.log(typeof obj);

4. Accessing properties in an object

Point access symbol.

o.name => 'terry'
o.age => 12

[] Accessor

o['name'] => 'terry'
o['age'] => 12

If you read a property that does not exist in the object, no error will be reported but undefined will be returned.

5. Add properties to the object

object.attribute = attribute value;

o.gender = 'female';

6. Deleting Object Properties

delete object.property;

delete o.gender;

7.Object root constructor

All objects directly or indirectly inherit Object and can call methods in the Object prototype

var o=new Object();
var arr = new Array();

8. Detection of object attributes

in Operator

This operator can be used to check whether an object contains a specified attribute

If yes, return true, otherwise return false

grammar:

"property name" in object

'name' in Obj;

9. Reference passing and value passing

Basic data types are passed by value: Basic data types store values ​​directly in the stack area of ​​​​memory

The variables in js are all basic data types stored in the stack memory. The values ​​are directly stored in the stack memory. The values ​​exist independently. Modifying one variable will not affect other variables.
var a=123;
var b=a;
a++;
console.log('a='+a); //124
console.log('b='+b); //123

Reference data types are passed by reference: the reference address of the reference data type is stored in the stack area, and the actual value is stored in the heap area. The reference address points to the space in the heap area.

Objects are stored in the heap memory. Every time a new object is created, a new space is created in the heap memory.

The variable stores the memory address of the object (the reference of the object). If two variables store the same object reference

When one variable modifies its properties, the other is also affected.

var obj = new Object();
obj.name='terry';
var obj2=obj;
 
//Modify the name attribute of obj obj.name='tom';
 
console.log(obj.name); //tom
console.log(obj2.name); //tom

When comparing two primitive data types, the comparison value

When comparing two reference data types, it compares the memory addresses of the objects.

It will also return false if the two objects are identical but have different addresses.

var c=10;
var d=10;
console.log(c==d); //true
 
 
var obj3=new Object;
var obj4=new Object;
obj3.name='tom';
obj4.name='tom';
console.log(obj3==obj4); //false

10. Object serialization (converting objects into strings)

1) Conventional conversion

obj.tostring()

2) Convert to json string

JSON.stringify(obj)

3) Query String

var qs = require('querystring'); //Introduce node.js module

qs.stringify(obj) =>name=tom&age=12

11. Enhanced for loop (for in)

Iterating over objects

grammar:
for(var variable in object){ }
for...in statement The loop will execute several times if there are several attributes in the object.

Each time it is executed, a property name in the object is assigned to the variable

var obj = {
    name:'Sun Wukong',
    age:18,
    gender:'male',
    address:'Huaguoshan'
}
 
for(var n in obj){
    console.log('Property name:'+n);
    console.log('Property value: '+obj[n]);
    }

This is the end of this article about Script Objects in Java (details). For more relevant JavaScript object content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript data visualization: ECharts map making
  • Super detailed basic JavaScript syntax rules
  • Detailed explanation of mandatory and implicit conversion of types in JavaScript
  • JavaScript implements changing the color of a web page through a slider
  • Detailed explanation of the differences between var, let and const in JavaScript es6
  • Detailed explanation of the this pointing problem in JavaScript
  • JavaScript function call, apply and bind method case study
  • Detailed explanation of the use of Arguments object in JavaScript
  • JavaScript CollectGarbage Function Example
  • Detailed explanation of BOM and DOM in JavaScript
  • JavaScript setTimeout and setTimeinterval use cases explained
  • JavaScript timer to achieve limited time flash sale function
  • JavaScript to implement limited time flash sale function

<<:  How to run the springboot project in docker

>>:  Windows Server 2012 No Remote Desktop License Server can provide a license, and the remote session is disconnected

Recommend

Understanding of haslaylout and bfc parsing

1. haslayout and bfc are IE-specific and standard ...

Install Zookeeper under Docker (standalone and cluster)

After starting Docker, let's take a look at t...

How to install Jenkins on CentOS 8

To install Jenkins on CentOS 8, you need to use t...

On good design

<br />For every ten thousand people who answ...

How to monitor array changes in Vue

Table of contents Preface Source code Where do I ...

How to set up swap partition SWAP in Linux 7.7

The Swap partition of the Linux system, that is, ...

Linux remote login implementation tutorial analysis

Linux is generally used as a server, and the serv...

MySQL batch removes spaces in a certain field

Is there any way to remove spaces from a certain ...

How to set up automatic daily database backup in Linux

This article takes Centos7.6 system and Oracle11g...

A brief discussion on the use of React.FC and React.Component

Table of contents 1. React.FC<> 2. class xx...

Let's talk about the Vue life cycle in detail

Table of contents Preface 1. Life cycle in Vue2 I...

Nest.js authorization verification method example

Table of contents 0x0 Introduction 0x1 RBAC Imple...