1 | // objects
|
2 |
|
3 | // load math.js (using node.js)
|
4 | var math = require('../index');
|
5 |
|
6 | // create an object. Keys can be symbols or strings
|
7 | print(math.eval('{x: 2 + 1, y: 4}')); // {"x": 3, "y": 4}
|
8 | print(math.eval('{"name": "John"}')); // {"name": "John"}
|
9 |
|
10 | // create an object containing an object
|
11 | print(math.eval('{a: 2, b: {c: 3, d: 4}}')); // {"a": 2, "b": {"c": 3, "d": 4}}
|
12 |
|
13 | var scope = {
|
14 | obj: {
|
15 | prop: 42
|
16 | }
|
17 | };
|
18 |
|
19 | // retrieve properties using dot notation or bracket notation
|
20 | print(math.eval('obj.prop', scope)); // 42
|
21 | print(math.eval('obj["prop"]', scope)); // 42
|
22 |
|
23 | // set properties (returns the whole object, not the property value!)
|
24 | print(math.eval('obj.prop = 43', scope)); // 43
|
25 | print(math.eval('obj["prop"] = 43', scope)); // 43
|
26 | print(scope.obj); // {"prop": 43}
|
27 |
|
28 |
|
29 | /**
|
30 | * Helper function to output a value in the console. Value will be formatted.
|
31 | * @param {*} value
|
32 | */
|
33 | function print (value) {
|
34 | var precision = 14;
|
35 | console.log(math.format(value, precision));
|
36 | }
|