繁体   English   中英

为什么这些手写笔哈希的行为不一样?

[英]why don't these stylus hashes behave the same?

在我的Javascript中:

// styl is the stylus compiler instance
styl.define('passedHash', new stylus.nodes.Object({
  top: 0,
  right: 2,
  bottom: 5,
  left: 20
}));

在我的.styl文件中:

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')

编译后的输出变为:

.fooBar {
  nodeType: 'object';
  padding: 0px;
  nodeType: 'object';
}

如果我取消该手写笔最后一行,我希望写出填充规则完全相同passedHash ,因为它是localHash 但是,手写笔崩溃了:

TypeError: expected "unit" to be a unit, but got null:null

为什么? 编译器知道它们都是对象...对我来说,它们似乎是完全相同的对象...。

好吧,根据Object节点的来源( https://github.com/LearnBoost/stylus/blob/master/lib/nodes/object.js ),您无法将JS对象传递给其构造函数。 因此,您的passedHash为空,您将收到该错误。 但是,您可以使用以下代码将真正的JS对象强制为Stylus对象:

styl.define('passedHash', {
  top: 0,
  right: 2,
  bottom: 5,
  left: 20
}, true); // <-- true for raw coercion

或更详细的版本:

styl.define('passedHash', stylus.utils.coerceObject({
  top: 0,
  right: 2,
  bottom: 5,
  left: 20
}, true));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM