'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
यह प्रश्न [पहले से ही उठाया गया है] (http://stackoverflow.com/questions/34605185/failing-to-subclass-builtin-string-object)। असल में नोड सबक्लासिंग बिल्ट-इन्स का पूरी तरह से समर्थन नहीं करता है। मुख्य समस्या यह है कि यह वास्तव में कभी भी 'रिवर्सस्ट्रिंग' उदाहरण नहीं बनाता है। नोड सिर्फ 'आरएस' को 'स्ट्रिंग' – CodingIntrigue