UNPKG

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