function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
Some other answers modify String.prototype
(this answer used to as well), but I would advise against this now due to maintainability (hard to find out where the function is being added to the prototype
and could cause conflicts if other code uses the same name / a browser adds a native function with that same name in future).
(其他一些答案修改了String.prototype
(这个答案也曾经使用过),但是由于可维护性,我现在不建议这样做(很难找出将函数添加到prototype
,如果其他代码使用相同的代码,可能会导致冲突。名称/浏览器会在将来添加具有相同名称的本机功能)。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…