// 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];
}
};
最佳答案
箭头函数有词法绑定(bind),所以你不能按你想要的方式使用this
。如果您有 this
是未定义的,您无法读取它的“替换”属性。
关于javascript - 如何将这个原型(prototype)函数转换成es6?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50476640/