没有子类的内置String对象 [英] Failing to subclass builtin String object
问题描述
我一直在尝试使用Node 5.3.0对ES2015中的内置String对象进行子类化。我正在使用一堆和谐标志来运行代码,这是完整的命令: 鉴于该规范具体说明了String对象是可子类的(请参见 重现问题的代码如下: 上面的代码演示了原型链没有像我所期望的那样设置。但是,如果我使用下面的代码手动修复 我很好奇知道为什么继承不能像我所期望的那样工作。 我还没有找到一个确定的答案但是我的快速而肮脏的解决方案目前一直在运作,所以我将把它作为遇到同样问题的人的答案。 您可以在您的 I've been experimenting with subclassing the built-in String object in ES2015 using Node 5.3.0. I'm running the code untranspiled using a bunch of harmony flags. Here's the full command:
Given that the spec specifically says the String object is made to be subclassable (See section The code to reproduce the issue follows: The code above demonstrates that the prototype chain isn't being setup as I would expect. However, if I manually fix the
I'm really curious to know why the inheritance is not working as I'd expect. I've yet to find a definitive answer to the question, but my quick and dirty solution has been working for the moment so I'll leave it as the answer for anyone who runs into the same problem. You can fix the prototype chain when inheriting from the String built-in using the following line in your The 这篇关于没有子类的内置String对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
节点--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 code $ c
$ b 21.1 .1 String构造函数
),我正在努力地了解这是否是我做错的事情,或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不是一个函数
__ 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
构造函数()
:
Object.setPrototypeOf(this,new.target.prototype);
new.target.prototype
bit确保您应该继承自己的类型,原型链将继续正确。 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
21.1.1 The String Constructor
), I'm struggling to understand if this is something that I'm doing wrong or a bug in Node or maybe even 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__
using the code below, everything works correctly.'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
bit ensure that should you inherit further from your own type the prototype chain will continue to be correct.