在 2021/6/14 上午4:10, Saúl Ibarra Corretgé 写道:
Hey folks,Acorrding to MDN document (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length), "|length| is a read-only data property of string instances." Maybe quickjs follows ES standard more strictly?
While running some code through QuickJS I ran into a difference in behaviour with regards to V8 and SpiderMonkey.
This code:
"use strict";
function SafeString(val) {
if (typeof val !== 'string') {
return val;
}
this.val = val;
this.length = val.length;
}
SafeString.prototype = Object.create(String.prototype, {
length: {
writable: true,
configurable: true,
value: 0
}
});
SafeString.prototype.valueOf = function valueOf() {
return this.val;
};
SafeString.prototype.toString = function toString() {
return this.val;
};
const s = new SafeString('test123');
console.log(s);
Works on V8 and SpiderMonkey but fails with this error on QuickJS:
TypeError: 'length' is read-only
at SafeString (testString.js:9)
at <eval> (testString.js:28)
Looks like the length property is not overriden properly?
Cheers,