1

I cannot figure out how to isolate/encapsulate a function so that only arguments and variables made within the function were accessible. For example if I had:

let greeting = "hello there";

function sayStuff(){
    console.log(greeting);
}

sayStuff();

Code above would print "hello there" as expected, but I want to make it so nothing used within sayStuff() could come from the outside unless parsed in as an argument. So I want to make it so that sayStuff() threw and error or printed null because greeting is not defined.

I looked into scope and many links and previous posts referred to doing

(function() {
      //your code here
    })();

as solution, but this was not the case at least with me, when having code:

const untouchableNumber = 5;
(function() {
      //your code here
      console.log(untouchableNumber);
    })();

it still printed 5 even though untouchableNumber was outside of its brackets and not one of the parameters.

Basically speaking I am trying to make function its own thing, and limit what scope it can access and what it can't.

Vadim Kim
  • 129
  • 12
  • 3
    That's not how scoping in JS works. JS _always_ has access to broader scoped symbols, unless you locally shadow them (e.g. you make a same-named variable inside your function). The closest you can get based on your description is to use JS modules and not set any global variables (which is usually a good idea anyway, if you're trying to write robust code). – Mike 'Pomax' Kamermans Aug 23 '19 at 00:03
  • So does it mean that I have no way of restricting a function on what it can or cannot access? – Vadim Kim Aug 23 '19 at 00:08
  • 1
    Effectively, so the real question is "why?" because if you write well behaved code, the fact that code inside some scoped context has access to everything in the higher scoped context is perfectly fine. Write your function normally, and they should never reach out (let alone need to reach out) of their own scope. – Mike 'Pomax' Kamermans Aug 23 '19 at 00:11
  • @Mike'Pomax'Kamermans in my specific case I want to allow people to write code for their own ai (ie you write some code and program parses it through an eval()) for a game, but I don't want them to mess with rules. For example I want them to be able to only use designated function and perform logic with specific variables, but am unable to do so because without limiting the scope people could just change any rule of the game – Vadim Kim Aug 23 '19 at 00:28
  • 1
    never, _ever_, allow code to run through `eval()` (or `New Function()`, or by passing JS code as string into `setInterval` or `setTimeout`). There's a very good reason we added blocking for those things using CSP. If you need to run user code, run it in an iframe _loaded of a **different domain**_ so that your user's code cannot, in any way, access your own page code, or cookies, or localStorage, or _anything_ else that isn't only exactly as much as their code needs in order to run. – Mike 'Pomax' Kamermans Aug 23 '19 at 15:18
  • Possible duplicate of [How to cut off function from global scope](https://stackoverflow.com/questions/57616796/how-to-cut-off-function-from-global-scope) – Kevin Workman Aug 23 '19 at 21:49

0 Answers0