Object Literals
Aug 07, 2024 am 10:01 AMObjects are collections of key-value pairs (properties).
To create an object we can do as follows:
let person = { firstName: 'Megan', isOnVacation: true, favFoods: ['plum', 'salad', 'fritos'] }
To access values within the object we can:
console.log(person.isOnVacation); // true console.log(person['isOnVacation']); // true
We can also make an object that contains objects:
let student = { name: 'Jimmy', exams: { artFinal: 'A', mathFinal: 88 } }
To access a value in the object's object, we can:
console.log(student.exams.artFinal); // 'A'
Also, we can make arrays containing objects:
let friends = [ { name: 'Sorour', favColor: 'blue', metAt: 'college' }, { name: 'Timothy', favColor: 'red', metAt: 'Starbucks' } ];
To access a value in the second object, we can call:
console.log(friends[1].favColor); // 'red'
To update a value in the second object, we can:
friends[1].name = 'Tim'; console.log(friends[1].name); // Tim
The above is the detailed content of Object Literals. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial

8 Stunning jQuery Page Layout Plugins

Improve Your jQuery Knowledge with the Source Viewer

10 Mobile Cheat Sheets for Mobile Development
