javascript - why don't these stylus hashes behave the same? -
in javascript:
// styl stylus compiler instance styl.define('passedhash', new stylus.nodes.object({ top: 0, right: 2, bottom: 5, left: 20 }));
in .styl file:
localhash = { top: 0 right: 2 bottom: 5 left: 20 } .foobar nodetype typeof(localhash) padding unit(localhash['top'], 'px') nodetype typeof(passedhash) padding: unit(passedhash['top'], 'px')
the compiled output becomes:
.foobar { nodetype: 'object'; padding: 0px; nodetype: 'object'; }
if uncomment last line in stylus, expect padding rule written out same passedhash
localhash
. instead, stylus crashes:
typeerror: expected "unit" unit, got null:null
why? compiler knows they're both objects... appear same exact object me....
well, according source of object
node (https://github.com/learnboost/stylus/blob/master/lib/nodes/object.js) cannot pass js object constructor. passedhash
empty , error. however, can coerce real js object stylus object using code:
styl.define('passedhash', { top: 0, right: 2, bottom: 5, left: 20 }, true); // <-- true raw coercion
or more verbose version:
styl.define('passedhash', stylus.utils.coerceobject({ top: 0, right: 2, bottom: 5, left: 20 }, true));
Comments
Post a Comment