dynamic-scope

Create dynamic scope in JavaScript


License
MIT
Install
npm install dynamic-scope@0.1.2

Documentation

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