子类化本机对象

Subclassing native objects

本文关键字:对象 本机 子类      更新时间:2023-09-26

我想用附加的方法创建我自己的RegExp子类。这是我的方法的最简化版本:

// Declare the subclass
function subRegExp(){}
// Inherit from the parent class
subRegExp.prototype = new RegExp();
// Create a new instance
regex = new subRegExp('[a-z]', 'g');

但是我无法创建一个新的实例。

这告诉我ECMAScript不支持本地对象的子类化,但它已经5年了,所以我希望现在有一些选择。

我怎样才能做到这一点?

编辑:这样可以吗?还是会遇到一些问题?
function subRegExp(str, flags){
    var instance = new RegExp(str, flags);
    // Custom method
    instance.setFlags = function(flags){
        return new subRegExp(this.source, flags);
    }
    return instance;
}
regex = new subRegExp('[a-z]', 'g');

包装器是您的好朋友,也是在不使用继承的情况下提供扩展功能的通用解决方案。

var MyRegexClass = function(regExpInstance) { 
  this.originalRegex = regExpInstance;
};
// Replicate some of the native RegExp methods in your wrapper if you need them.
MyRegexClass.prototype.test = function(str) {
    return this.originalRegex.test(str);
};
MyRegexClass.prototype.exec = function (str) {
    return this.originalRegex.exec(str);
};
// Now add in your own methods.
MyRegexClass.prototype.myCustomFunction0 = function () { 
    // this method does something with this.originalRegex
};
MyRegexClass.prototype.myCustomFunction1 = function () {
    // this method also does something with this.originalRegex
};
// Example usage
var matchDavids = new MyRegexClass(/David/);
// this call works, because my class provides the .test() method.
var hasMatch = matchDavids.test('David walked his dog to the park.');
// this call does not work, because my class does not expose the .compile() method.
matchDavids.compile();
// I would need to provide a .compile() method on MyRegexClass that calls to
// the originalRegex.compile().

是的,你失去了继承链。MyRegexClass不继承本机RegExp。根据我的经验,包装器比基于继承的扩展更容易测试和维护。

恐怕还是不行。

但是,您可以通过使用自定义包装器对象类来模拟一些所需的功能。在构造函数中使用封装,给它一个RegExp对象作为私有字段(Javascript最接近的东西)。

我试过了:

// Declare the subclass
function subRegExp(){}
// make your object inherit from regex object
subRegExp.prototype = Object.create( RegExp.prototype );
var x = new subRegExp();
// see if your custom object inherited the RegExp properties/functions/methods
console.dir( "compile" in x );
console.dir( x.compile );
输出:

true
function compile() { [native code] }

是的,现在在ES6中是可能的:

class R extends RegExp {}
var r = new R("baz", "g");
return r.exec("foobarbaz")[0] === "baz" && r.lastIndex === 9;

我们在ES6兼容表中有一个测试,你可以看到哪些实现支持它。

我将尝试更新我的博客文章(你引用的)关于数组子类化在ES5的某个时候很快。