Objects and Properties
Object Literals
Create a small record with named fields.
Object Literals
object_literal.js
const name = "Ada";
const score = ({ name: name, points: 7 }).points;
const label = ({ name: name, points: 7 }).name + ":" + score;
console.log("name=" + name);
console.log("score=" + score);
console.log("label=" + label);
Build a Small Record
namestarts asAda.- The object stores
nameandpoints. - Reading
.pointsgives7. - Reading
.namehelps buildAda:7. | Property | Value | | --- | --- | |name|Ada| |points|7| | label |Ada:7|
object-literal
An object literal groups related values under property names. Reading those properties gives each field back as a normal value.
Exercise: object_literal.js
Create an object with name and points, then print the score and a name-score label