举例:已知有字符串foo=”get-element-by-id”,写一个function将其转化成驼峰表示法”getElementById”。
<script>
function change(foo) {
var arr = foo.split('-');
for (var i = 1; i < arr.length; i++) {
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
}
console.log(arr.join(''));
}
change("get-element-by-id");
change("hello-world");
</script>
以上代码运行结果为:
getElementById
helloWorld