// 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未定义TypeError:无法读取未定义属性的“替换”
我是如何使用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];
}
};发布于 2018-05-22 21:28:28
箭头函数具有词法绑定,因此不能以您想要的方式使用this。在这种情况下,this是未定义的,并且无法读取属性“替换”。
发布于 2018-05-22 21:31:17
问题是您使用的是Arrow函数。
箭头函数表达式在词汇上绑定this值。因此,该值绑定到undefined。你必须使用正常的功能。
https://stackoverflow.com/questions/50476640
复制相似问题