I spend a good amount of time looking at JavaScript framework source code. Regardless of which frameworks you have allegiance to, you can learn an awful lot by looking under the hood of widely used code collections. One of many handy snippets can be found within the MooTools source code: functions to camelize and hyphenate strings so that your own min framework can accept either form of CSS setter or getter. Here are the functions in all of their glory.
我花了大量时间研究JavaScript框架源代码。 无论您使用哪种框架,都可以通过查看广泛使用的代码集合的幕后知识来学习很多知识。 您可以在MooTools源代码中找到许多方便的摘录之一:驼峰化和断字字符串的函数,以便您自己的min框架可以接受CSS setter或getter的形式。 这是其所有功能的功能。
JavaScript (The JavaScript)
As you could probably guess, this task is best accomplished with regular expressions:
您可能会猜到,最好用正则表达式完成此任务:
function camelize(str) { return (str + "").replace(/-\D/g, function(match) { return match.charAt(1).toUpperCase(); });}camelize("border-bottom-color"); // "borderBottomColor"function hyphenate(str) { return (str + "").replace(/[A-Z]/g, function(match) { return "-" + match.toLowerCase(); });}hyphenate("borderBottomColor"); // "border-bottom-color"
A couple of really handy JavaScript String to corresponding String format functions. Instead of expecting strings in only one format, your mini library can now accept both!
几个非常方便JavaScript String与对应的String格式函数。 现在,您的迷你图书馆不再只希望使用一种格式的字符串,而是可以接受两种格式!
翻译自: