Jun*_*uce 1 javascript string uppercase
我正在尝试编写一个字符串原型来检查字符串是否全部大写。这是我到目前为止所拥有的,我不确定为什么这不起作用。
String.prototype.isUpperCase = function(string) {
if(string === string.toUpperCase()) {
return true;
}else{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望它像这样工作:
'hello'.isUpperCase() //false
'Hello'.isUpperCase() //false
'HELLO'.isUpperCase() //true
Run Code Online (Sandbox Code Playgroud)
原型方法接收 in 中的实例this,而不是您的代码所期望的第一个参数。尝试这个:
String.prototype.isUpperCase = function() {
return String(this) === this.toUpperCase();
}
Run Code Online (Sandbox Code Playgroud)
该String(this)调用确保它this是一个字符串基元而不是一个字符串对象,因为该运算符不会被识别为等于===。
| 归档时间: |
|
| 查看次数: |
1522 次 |
| 最近记录: |