I am looking into the Object.create to create a custom RegExp class. I want to use pure JS and not JQuery or other library. After spending time researching on MDN, here and other places, I find that my expectations and understand are still not quite adequate for my task.
So far I have this:
( function ( SEAC ) {
var _NativeRegExp = RegExp,
_NativeRegExpProto = _NativeRegExp.prototype;
SEAC.RegExp = function ( ) { _NativeRegExp.apply( this, arguments ); };
SEAC.RegExp.prototype = Object.create( _NativeRegExpProto );
} )( self.SEAC || ( self.SEAC = {} ) );
If I use it in the following code:
var re = new SEAC.RegExp( "[aeiou]+", 'g' );
var match = 'The quickening'.match( re );
I get this response:
TypeError: Method RegExp.prototype.toString called on incompatible receiver [object Object]
I hope this the intention and question is clear enough.
Anyone can explain to me what I am doing wrong and/or suggest how to do this? Or maybe my approach is not the best way, suggest another. I have done this before without using Object.create, but I wish to see if this is a better way to deal with inheritance and class creation.
Why I want to do this is of course I want to customize the class and some of the native methods.
RegExp.prototypeto objects that are not instances ofRegExp: "a TypeError exception is thrown if thethisvalue is not an object or an object for which the value of the[[Class]]internal property is not"RegExp"." es5.github.io/#x15.10.6 . So I guess it's not possible to inherit fromRegExp.