子类化本机对象
Subclassing native objects
我想用附加的方法创建我自己的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的某个时候很快。
相关文章:
- 如何创建行为类似于本机对象的对象
- 如何在javascript中检测本机浏览器/引擎对象
- TypeError:对象函数Object(){〔本机代码〕}没有方法'方法'
- 使用方法和属性定义javascript对象的最佳方法是什么?(如果可能以本机方式)
- 调用本机原型方法时扩展或继承对象原型
- 在 JavaScript 中依赖全局本机对象是否安全,尤其是 Object 对象
- 我怎么知道本机 JS 对象没有被篡改
- 将本机数组/对象转换为 Ember.Array/Ember.Object 的最佳实践
- 对象函数 Date() { [本机代码] } 在 Chrome 中使用 Date.js 时没有方法“compareTo”
- 通过带有对象子级的数组进行 React 本机映射 - 工作方式与在 react web 中不同
- 如何在 nodejs 中全局添加方法到 JSON、Date 或其他“本机”对象
- 使用原型来扩展本机对象不好
- 渲染时,未定义不是React本机中的对象
- 将本机浏览器事件对象转换为jQuery事件对象
- JavaScript:主机对象也是本机对象的示例是什么
- 在本机错误对象调用函数's结构
- 有没有一种方法可以在javascript中隔离本机对象扩展
- 实验:扩展本机对象
- 如何覆盖javascript本机对象的内置方法
- 本机事件对象的自定义方法