Error expanding 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()); 

when I run this code, I encounter an error:

 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 

I can not find the root cause of this error.

output console.log(rs); equals String {0: "w", 1: "a", 2: "n", 3: "g", 4: "y", 5: "a", 6: "n", 7: "g", length: 8, [[PrimitiveValue]]: "wangyang"}] .

Here is my version of node:

 C:\Users\elqstux\Desktop>node -v v5.3.0 
+6
source share
1 answer

String is not currently subclassed in Node 5.3, according to:

https://kangax.imtqy.com/compat-table/es6/#test-miscellaneous_subclassables

Your example should work fine in Firefox 45+ and Edge 13+

+2
source

All Articles