dojo.setObject
<script src="../../js/dojo.js"></script>
defined in dojo/_base/_loader/bootstrap.js
Useful for longer api chains where you have to test each object in
the chain, or when you have an object reference in string format.
Objects are created as needed along path
. Returns the passed
parameter | type | description |
---|---|---|
name | String | Path to a property, in the form "A.B.C". |
value | Object | if setting is successful or `undefined` if not. |
context | Object | Optional. Optional. Object to use as root of path. Defaults to `dojo.global`. |
Examples
Example 1
set the value of foo.bar.baz
, regardless of whether
intermediate objects already exist:
dojo.setObject("foo.bar.baz", value);
Example 2
without dojo.setObject
, we often see code like this:
// ensure that intermediate objects are available
if(!obj["parent"]){ obj.parent = {}; }
if(!obj.parent["child"]){ obj.parent.child= {}; }
// now we can safely set the property
obj.parent.child.prop = "some value";
wheras with dojo.setObject
, we can shorten that to:
dojo.setObject("parent.child.prop", "some value", obj);