在 ES6 中扩展字符串时出错
The error when extending String in ES6
'use strict';
class ReverseString extends String {
reversed() {
let res = '';
for (let i = this.length - 1; i >= 0; --i) {
res += this[i];
}
return res;
}
}
let rs = new ReverseString("wangyang");
console.log(rs.reversed());
当我运行此代码时,我遇到一个错误:
C:'Users'elqstux'Desktop>node wy.js
C:'Users'elqstux'Desktop'wy.js:14
console.log(rs.reversed());
^
TypeError: rs.reversed is not a function
at Object.<anonymous> (C:'Users'elqstux'Desktop'wy.js:14:16)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:430:10)
at startup (node.js:141:18)
at node.js:980:3
我找不到此错误的根本原因。
console.log(rs);
的输出是String {0: "w", 1: "a", 2: "n", 3: "g", 4: "y", 5: "a", 6: "n", 7: "g", length: 8, [[PrimitiveValue]]: "wangyang"}]
。
这是我的节点版本:
C:'Users'elqstux'Desktop>node -v
v5.3.0
String
目前在 Node 5.3 中不可子类化,根据:
https://kangax.github.io/compat-table/es6/#test-miscellaneous_subclassables
您的示例应该在 Firefox 45+ 和 Edge 13+ 上运行良好
相关文章:
- 比较Javascript中的两个字符串时出错
- 尝试组合字符串时出错
- 解析从js发送到Java的json字符串时出错
- 在javascript中将字符串转换为int时出错
- 根据64字符串绘制画布图片时出错
- angularjs[ng:areq]参数'fn'不是函数,在控制台中获取字符串时出错
- WebSQL:处理SQL时出错:数量为''语句字符串中的s与参数计数不匹配
- 在 AngularJS 中将字符串转换为对象时出错
- NaN Javascript 和 Jquery 尝试连接数字和字符串时出错
- 将对象转换为字符串 Javascript 时出错
- 在 ES6 中扩展字符串时出错
- 将字符串中的方括号文字与JS正则表达式匹配时出错
- 将字符串变量传递给方法时出错
- NodeJS Bug?执行在要返回的字符串之前带有换行符的返回语句时出错
- 在javascript中连接utf字符串时出错
- 转义字符串时出错
- 敲除js时出错字符串不是函数
- JQuery比较字符串时出错
- 使用JQuery解析JSON字符串时出错
- 将字符串转换为json对象时出错