What is setprototypeof?
The setprototypeof package is a simple module that allows you to set the prototype of an object, which can be useful for creating a chain of prototypes (i.e., inheritance) or for changing an object's prototype on-the-fly. It is a polyfill for the official ECMAScript 2015 (ES6) Object.setPrototypeOf method.
What are setprototypeof's main functionalities?
Setting the prototype of an object
This feature allows you to set the prototype of an object to another object. After setting the prototype, the object will have access to the properties and methods of the new prototype.
{"object": {"prop": "value"}, "prototype": {"newProp": "newValue"}}
Other packages similar to setprototypeof
object-assign
The object-assign package is used to copy the values of all enumerable own properties from one or more source objects to a target object. It is similar to setprototypeof in that it is used for manipulating objects, but it does not change the prototype chain.
lodash.assign
Lodash's assign function is similar to object-assign and is used to assign own enumerable properties of source objects to the destination object. Like object-assign, it does not deal with the prototype chain but rather with copying properties.
util
The util package is a collection of utilities originally for the Node.js standard library. It includes util.inherits, which is a method that allows you to set the prototype of a constructor to an object, creating a prototype chain similar to what setprototypeof does, but specifically for constructor functions.
Polyfill for Object.setPrototypeOf
A simple cross platform implementation to set the prototype of an instianted object. Supports all modern browsers and at least back to IE8.
Usage:
$ npm install --save setprototypeof
var setPrototypeOf = require('setprototypeof')
var obj = {}
setPrototypeOf(obj, {
foo: function () {
return 'bar'
}
})
obj.foo()
TypeScript is also supported:
import setPrototypeOf from 'setprototypeof'