【发布时间】:2018-05-22 21:23:19
【问题描述】:
// Converts snake-case to camelCase
String.prototype.toCamel = function () {
return this.replace(/(\-[a-z])/g, $1 => $1.toUpperCase().replace('-', ''));
};
当我执行以下操作时:
// Converts snake-case to camelCase
String.prototype.toCamel = () => this.replace(/(\-[a-z])/g, $1 => $1.toUpperCase().replace('-', ''));
我收到此错误:
modifiers.js:9 Uncaught TypeError: Cannot read property 'replace' of undefined
我如何使用 toCamel 函数:
// Add style to coin
export const setStyle = (id) => {
switch (id) {
case 'basic-attention-token': return style.basicattentiontoken;
case 'bitcoin-cash': return style[id.toCamel()];
case 'deepbrain-chain': return style[id.toCamel()];
case '0x': return style.zrx;
default: return style[id];
}
};
【问题讨论】:
-
问题的标题具有误导性。在您的示例中,您没有将任何内容转换为 ES6。您将常规函数替换为箭头函数,在这种情况下这没有任何意义。
-
一个箭头函数是es6
标签: javascript ecmascript-6 prototype