Skip to content

Extends the Object Prototype safely using ES6 Symbols

License

Notifications You must be signed in to change notification settings

nperez0111/objExtender

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

objExtender Build Status

Extends the Object Prototype safely using ES6 Symbols

This small utility library allows you to extend the Object prototype safely to put methods that all objects will be guarenteed to have (since it is an extension of the Object Prototype which all Objects inherit from).

To see why this is safe or not safe refer to my library

Install

$ npm install --save objextender

Usage

const extend = require( 'objextender' );

let get = extend( {

    keys: function () {

        return Object.keys( this )

    }

} )

let x = {
    a: 1,
    b: 2
}

x[ get.keys ]()
//=> [ 'a', 'b' ]

API

objextender(input, [options])

input

Type: object

input must be in a format where the values of any given key is a function

options

getHelperMethod

Type: boolean
Default: true

This allows you to choose whether you would like the provided get helper method which allows you to do this:

let please = extend( {

    grabX: function ( get ) {

        return get( 'x' );

    }

} )

let y = {
    x: 34
}

y[ please.grabX ]()
//=> 34
toExtend

Type: object(but really a class)
Default: Object (the class)

This allows you to choose what object you would like to be extending the prototype of safely. Like so:

let classyClass = function () {
        this.x = 'magic'
    };

let please = fn( {
        grabX: function ( get ) {
            return get( 'x' );
        }
    }, {
        toExtend: classyClass
    } )
    
let objectOfClass = new classyClass();
    
objectOfClass[ please.grabX ]()
//=> 'magic'

License

MIT © Nick The Sick

About

Extends the Object Prototype safely using ES6 Symbols

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages