@thetalmid/nject

1.0.0 • Public • Published

ŋject logo

Keeps your prototype chain healthy & sane.

Usage 💉

ŋ : Alt Gr + g

with Functions:

////////////////////////////////////////////////////////////////////////////////

import ŋ from './ŋject.js'

////////////////////////////////////////////////////////////////////////////////

let x = 'Hello'
let y = 'World'

const func = (...args) => { console.log(...args) }

const cloj = ŋ(func,
[
    '',
    ()=>(y)
])

cloj()  // Prints 'undefined World'
cloj(x) // Prints 'Hello World'

const anotherCloj = ŋ(func, [()=>(y)])

anotherCloj()  // Prints 'World undefined'
anotherCloj(x) // Prints 'World Hello'

x = 'Ciao'
y = 'Bella'

anotherCloj(x) // Prints 'Bella Ciao"

let a = 'Here'; let b = 'comes'
let c = 'the'; let d = 'sun'

const morningCloj = ŋ(func, [
    '',     // An empty string signals a slot to be filled with later arguments.
    ()=> b, // Bindings are declared through arrow functions with no parameters,
    '',     // followed by the target identifier.
    ()=>(d) // Parentheses & return statement optional.
])

morningCloj()     // Prints 'undefined comes undefined sun'
morningCloj(a, c) // Prints 'Here comes the sun'

////////////////////////////////////////////////////////////////////////////////

with Objects:

////////////////////////////////////////////////////////////////////////////////

import ŋ from './ŋject.js'

////////////////////////////////////////////////////////////////////////////////

function Person(hobbies) {
    this.hobbies = hobbies
}

let bob = new Person('gardening')
let alice = new Person()

alice = ŋ(alice, [()=> bob.hobbies])

console.log(alice['hobbies']) // Prints 'gardening'

const armoire = {
    dress: 'pretty',
    sandals: 'worn out'
}

alice = ŋ(alice, [
    ()=> armoire.dress,          // ŋ automatically names properties...
    (footwear)=> armoire.sandals // ...unless an alias is set.
])

console.log(alice.dress)    // Prints 'pretty'
console.log(alice.footwear) // Prints 'worn out'

alice.dress = 'ragged'
console.log(armoire.dress) // Prints 'pretty'
console.log(alice.dress)   // Prints 'ragged'

delete alice.dress       // Restores shadowed closure
console.log(alice.dress) // Prints 'pretty'

console.log(alice.hobbies) // Prints 'gardening'
                           // Closures compound.

////////////////////////////////////////////////////////////////////////////////

Readme

Keywords

none

Package Sidebar

Install

npm i @thetalmid/nject

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

6.55 kB

Total Files

5

Last publish

Collaborators

  • thetalmid