How to Update Literal JavaScript
Object literals are one of the mechanisms available in the JavaScript programming language to write object-oriented code. An object literal consists of a list (enclosed in curly braces) of comma-separated name/value pairs. The name is a JavaScript string; the value can be any JavaScript object, including another (nested) object literal. Each name is separated from its value by a colon. An object literal represents a mapping from names to the corresponding values. You can update the contents of a literal you have created in your JavaScript code.
Instructions
-
-
1
Create a JavaScript literal by assigning name/value pairs to it, as in the following sample code:
var myCar = {
brand: "Ford",
engine: "V6",
cylinders: 6
}
The example has three name/value pairs. Two values are alphanumeric, one is numeric.
-
2
Update a JavaScript literal by adding a name/value property to it, as in the following sample code:
myCar['doors'] = 4
-
-
3
Update a JavaScript literal by modifying the value associated to an already-existing name, as in the following sample code:
myCar['cylinders'] = 5
The most recent value assignment supersedes all previous assignments to the same name.
-
1