无法子类化内置 String 对象

Failing to subclass builtin String object

我一直在尝试使用 Node 5.3.0 对 ES2015 中的内置 String 对象进行子类化。我是 运行 使用一堆和谐标志未转译的代码。这是完整的命令: node --harmony --harmony_modules --harmony_destructuring --harmony_rest_parameters --harmony_arrow_functions --harmony_spreadcalls --harmony_object --harmony_default_parameters --harmony_new_target --harmony_reflect --harmony_modules ~/t.js

鉴于规范明确指出 String 对象是可继承的(参见 21.1.1 The String Constructor 部分),我很难理解这是我做错了什么还是 Node 中的错误甚至可能是 V8。

重现问题的代码如下:

'use strict';

class Str extends String {
  capitalize() {
    return `${this.slice(0, 1).toUpperCase()}${this.slice(1)}`;
  }
}

var s = new Str('asdf');

console.log(s.constructor);
//[Function: String]

console.log(s.__proto__)
//[String: '']

console.log(s.capitalize());
//TypeError: s.capitalize is not a function

上面的代码表明原型链没有按照我的预期进行设置。但是,如果我使用下面的代码手动修复 __proto__,一切正常。

'use strict';

class Str extends String {
  constructor(...args) {
    super(...args);
    Object.setPrototypeOf(this, new.target.prototype);
  }

  capitalize() {
    return `${this.slice(0, 1).toUpperCase()}${this.slice(1)}`;
  }
}

var s = new Str('asdf');
console.log(s.constructor);
//[Function: Str]

console.log(s.__proto__);
//Str {}

console.log(s.capitalize());
//Asdf

我真的很想知道为什么继承没有像我预期的那样工作。

我还没有找到这个问题的明确答案,但我的快速而肮脏的解决方案目前一直有效,所以我将把它作为遇到相同问题的任何人的答案。

您可以在 constructor() 中使用以下行从内置字符串继承时修复原型链:

Object.setPrototypeOf(this, new.target.prototype);

new.target.prototype 位确保如果您进一步继承您自己的类型,原型链将继续正确。