[quickjs-devel] Re: Failure to override String.length ?

  • From: ShaJunxing <shajunxing@xxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx, Saúl Ibarra Corretgé <s@xxxxxxxxxx>
  • Date: Thu, 17 Jun 2021 08:41:01 +0800

在 2021/6/14 上午4:10, Saúl Ibarra Corretgé 写道:

Hey folks,

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');


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?


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?

Other related posts: