Błąd przy przedłużaniu smyczkowy 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());
kiedy uruchomić ten kod, spotykam błąd:
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
nie mogę znaleźć przyczyny tego błędu.
Dane wyjściowe console.log(rs);
to String {0: "w", 1: "a", 2: "n", 3: "g", 4: "y", 5: "a", 6: "n", 7: "g", length: 8, [[PrimitiveValue]]: "wangyang"}]
.
Oto moja wersja węzeł:
C:\Users\elqstux\Desktop>node -v
v5.3.0
To pytanie [zostało już podniesione] (http://stackoverflow.com/questions/34605185/failing-to-subclass-builtin-string-object). Zasadniczo Węzeł nie w pełni obsługuje wbudowane podklasy. Głównym problemem jest to, że nigdy tak naprawdę nie tworzy "instanceof ReverseString". Węzeł po prostu widzi 'rs' jako' String' – CodingIntrigue