Access JavaScript's native Math library in Stylus

爱⌒轻易说出口 提交于 2019-12-11 02:36:03

问题


I lately asked a question if there was a possibility to calculate the square root in stylus.
After having an answer to this, I wondered, if there was a way to access JavaScript's native Math library in Stylus entirely.

Any ideas?


回答1:


There is actually a math function in Stylus (yet undocumented).

Usage is rather easy:

sqrt-of-2: math(2, 'sqrt')

The syntax is somewhat clumsy — the first argument is the argument you want to pass to the method, the second argument is the string name of the method.

For get the math prop like PI you'll need a somewhat private (but accessible) function -math-prop:

e: -math-prop('E')

I filled an issue in Stylus to write the docs on this bif and to provide a shortcut to -math-prop, so you can expect it to appear in Stylus in one of the next releases.




回答2:


I came up with a simple wrapper, that mapps JS globals to Stylus:
https://bitbucket.org/jkowalleck/stylus-jscoremapper


Sample code for Stylus:

// mapp javascript's Math to `math`
use('jsCoreMapper.js', {math:'Math'}) 

test-math
    PI math-PI
    sqrt-of-2 math-sqrt(2)


Output:

test-math {
  PI: 3.141592653589793;
  sqrt-of-2: 1.4142135623730951;
}


This suited my needs ... but ... Any ideas on this? Could this be improved?

来源:https://stackoverflow.com/questions/21113212/access-javascripts-native-math-library-in-stylus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!