create
The missing Native.create() functions that ECMA forgot.
create
is a module for node.js (and someday the browser too) that implements the
"missing" .create()
functions on the rest of the native data types in
JavaScript.
There's already Object.create()
. So why not Array.create()
? Or
Function.create()
? Well that's exactly what this module adds functionality for.
In essense, this gives you a clean interface for subclassing the native classes,
and also gets all the same benefits that Object.create()
gives you, like setting
the prototype at creation-time, and being able to pass an object descriptor in to
define additional properties.
When you require the module, you invoke it as a function and pass in any native
classes you want extended with a .create()
function. This can be done with
any of the native types.
Installation
Install with npm
:
$ npm install create
Example
Let's make a subclass of Array
, that includes a remove()
function:
Array// `Array2` is the prototype of our subclassvar Array2 = Object// remove() impl from http://ejohn.org/blog/javascript-array-removeArray2 {var rest = this;thislength = from < 0 ? thislength + from : from;return thispush;}// now we can create an instance of Array2var a = Array// add some items to itaa// [1, 2, 3, 'foo', 'bar']a// [1, 3, 'foo', 'bar']
Limitations
This module depends on the writability of the __proto__
property on objects.
From a browser standpoint, this module will only work in browsers where that is
true (__proto__
MAY be changed). In node this will always work.