dynamic-scope
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

dynamic-scope

Create dynamic scope in JavaScript with "with statement" trick.

It is a native ES2017 module, it provides complete dynamic scope implementation in non-strict mode and incomplete dynamic scope implementation in strict mode.

Of course, closure variables will be lost.

Installation

yarn add dynamic-scope

Usage

import createDynamicScopeExpression from 'dynamic-scope'
 
function someFuncShouldBeDynamicScope(...args) {
  console.log([message, ...args].join(' ') + '!')
}
 
const dynamicFuncExpression = createDynamicScopeExpression(someFuncShouldBeDynamicScope)
 
// Non-strict mode
;(() => {
  const message = 'Hello'
  const func = eval(dynamicFuncExpression)()
  func('World') // Hello World!
})()
 
// Strict mode
;(() => {
  'use strict'
  const func = eval(dynamicFuncExpression)({ message: 'Hello' })
  func('World') // Hello World!
})()

API

Table of Contents

createDynamicScopeExpression

Create dynamic scope in JavaScript with "with statement" trick.

Parameters

  • fn function The function needs to convert

Returns string An expression needs eval() to active, eval(expression) will return (context) => function

Dependencies (1)

Dev Dependencies (16)

Package Sidebar

Install

npm i dynamic-scope

Weekly Downloads

0

Version

0.1.2

License

MIT

Unpacked Size

166 kB

Total Files

23

Last publish

Collaborators

  • black_glory