2

How is it possible to override/hijack reference in scope chain?

If I have a function in another file (node env):

const name = 'name_local'
export default () => { return name }

From another file I want to import that function but override it's reference to name passing a new reference:

import nameFunc from '../otherFile'
const name = 'hijacked name reference'
nameFunc() // 'hijacked name reference'
CDspace
  • 2,639
  • 18
  • 30
  • 36
Cory Robinson
  • 4,616
  • 4
  • 36
  • 53

2 Answers2

2

You can't, the language doesn't provide a mechanism for you to do that.

That one sentence seems meager, but that's really it: The language doesn't let you. In specification terms, although the function has access to its enclosing lexical environment record, which has a binding for name, that reference (and in fact, lexical environment records in general) are not exposed to script code (and are allowed to not even literally exist in an implementation; they're a specification mechanism). So there's no way to get that informaton from the function, so no way to change it.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    ...fortunately. :-) – T.J. Crowder Feb 23 '17 at 18:24
  • @guest271314: Doing that would completely replace the function. If it uses anything from its source context that you don't replace, the function breaks. If it doesn't (you replace everything), there's no point: Just use your own function. – T.J. Crowder Feb 23 '17 at 18:32
  • Yes, possibly no point, though is "You can't" entirely accurate? Unless not gathering Question fully, here; which probably am not. – guest271314 Feb 23 '17 at 18:33
  • 1
    @guest271314: Yes, in my view, for the reasons stated above. – T.J. Crowder Feb 23 '17 at 18:35
  • 1
    Thx everyone - I understand the security implication this would expose the language too :-| I'm hitting a point in writing unit tests for a library but advised not to change any source code while also achieving 100% coverage :-( Was looking for a small workaround, if possible – Cory Robinson Feb 23 '17 at 18:43
  • 1
    @CoryRobinson: LOL, nice requirements. :-) – T.J. Crowder Feb 23 '17 at 18:45
  • @T.J.Crowder Was not aware that `delete` does not delete `const` variable? `const name = "name_local"; delete name // true; console.log(name) // "name_local"`? Is it possible to delete a variable declared using `const`? – guest271314 Feb 23 '17 at 18:49
  • http://stackoverflow.com/questions/42424019/is-it-possible-to-delete-a-variable-declared-using-const – guest271314 Feb 23 '17 at 19:00
1

Just do it this way:

const name = 'name_local';
export default (input = name) => { return input }

other file:

import nameFunc from '../otherFile'
const name = 'name_other';
nameFunc(name)
cyr_x
  • 13,987
  • 2
  • 32
  • 46
  • Thx - not a solution to my application, although a simple answer. I'm approaching code that I don't have access to change the function signature ie: adding a param w/ default. – Cory Robinson Feb 23 '17 at 18:40