jQuery源码中的驼峰命名jQuery.camelCase

来源:互联网 发布:notebook软件 编辑:程序博客网 时间:2024/05/17 20:50

仁怀,感恩,真心感谢那些默默付出的人们。

var     // 防止 $('p').css('-ms-transform', 'scale(1)') 出错    rmsPrefix = /^-ms-/,    // 注意细节小括号           rdashAlpha = /-([a-z])/g,    //源码中的参数名字稍作修改下更加好理解些     fcamelCase = function( dashAlpha , Alpha ) {        return letter.toUpperCase();    };  jQuery.extend ({     // 将虚线转换为camelCase;由CSS和数据模块使用     // 支持:IE <= 9 - 11,Edge 12 - 13     // 微软忘记了他们的供应商前缀 [(#9572)](https://bugs.jquery.com/ticket/9572)   camelCase: function( string ) {   return string.replace( rmsPrefix, "ms-" )                .replace( rdashAlpha, fcamelCase );                                 }                })   调用的话:    var str = '-ms-hello-jack-';    var camel   = jQuery.camelCase(hello)        camel;   //  'msHelloWorldJack' 小提示: *在jQuery源码中经常会看到 #3433  这样的字符可以在 [jQuery Bugs](https://bugs.jquery.com/) 中的搜索框中输入这串数字查询更多信息* **小窍门 细节是魔鬼**      1.理解源码中的名字           例如 rmsPrefix  r说明是个正则 msPrefix指的是Microsoft 前缀               连起来可以理解为为了匹配微软下的前缀使用的正则               fcamelCase f可以理解为回调函数的简称 camelCase可以理解为驼峰化     2.学好英语是理解代码的不二之选,       建议英语不好的同学可以通过google翻译去学习,       安装的话需要翻墙在Chrome中找插件                3.源码中的         fcamelCase = function( all , letter ) {        return letter.toUpperCase();    };       我改为了        fcamelCase = function( dashAlpha , Alpha ) {        return letter.toUpperCase();    };        原因如下        例如:如果是all, letter的话        var   fcamelCase = function( all, letter ) {           console.log(all, letter);           return letter.toUpperCase();       }        var str = '-ms-hello';        fcamelCase(str);  //   -h h                                        -h   可以语义化为 dashAlpha 相匹配                        h   可以语义化为 Alpha                         这样的命名的话可以突出正则               var    rdashAlpha = /-([a-z])/g 中的小括号的细节点         实际中            var str = 'hello-world',                 rdashAlpha = /-([a-z])/,                fcamelCase = function (dashAlpha, Alpha, startIndex, all);            str.replace.(rdashAlpha, fcamelCase);         其实  fcamelCase() , 默认的有四个参数 ,         最后一个表示  'hello-world'  将其名字定位为 all 较合适些   
0 0
原创粉丝点击