现在的位置: 首页 > 综合 > 正文

jQuery源码分析9: 驼峰式命名方法jQuery.camelCase

2019年10月30日 ⁄ 综合 ⁄ 共 700字 ⁄ 字号 评论关闭

jQuery源码分析: 驼峰式命名方法jQuery.camelCase

// Matches dashed string for camelizing 匹配由虚线分割的字符串并改成驼峰式命名法
var rdashAlpha = /-([a-z]|[0-9])/ig,
    rmsPrefix = /^-ms-/,

    // Used by jQuery.camelCase as callback to replace()
    fcamelCase = function( all, letter ) {
        return ( letter + "" ).toUpperCase();
    };

camelCase: function( string ) {
    return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase );
},

举例:
var rdashAlpha = /-([a-z]|[0-9])/ig,
      rmsPrefix = /^-ms-/;

    // Used by jQuery.camelCase as callback to replace()
var fcamelCase = function( all, letter ) {
        return ( letter + "" ).toUpperCase();
};

var camelCase = function( string ) {
    return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase );
};

alert(camelCase("hello-world-baby"));//< 输出"helloWorldBaby"

抱歉!评论已关闭.