Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

Why are JavaScript Symbol references safe from collisions?

I have spent many hours doing my research - I'm a total dummy, but I'm not being lazy. I have Reddit/SO/Googled extensively...

Also, I am new to posting on SO and I'm sure I'm doing something wrong - sorry.

Please let me know if I'm on the right track:

"If you don’t have the reference for the Symbol, you just can’t use it"

  • Does this mean that you need the variable to which you assigned the symbol as a value (when symbol was created)? And is that useful BECAUSE the scope of that variable is limited? So it's not like something outside the scope of the variable could accidentally use the reference name. There is no NAME to the symbol to be guessed, right? The reference is just a pointer, and the only way to use the pointer is if you are within scope of where the pointer was defined. Is that it?

I'm confused as to why collisions couldn't happen with the reference. I believe this has to do with my ignorance of how different pieces of code interact... Could someone not give my reference as an argument at the wrong place/time, and have it collide with my symbol? Are my variables/scope naturally untouchable by other peoples code, or do I have to purposefully declare variable with symbol value somewhere specific... Ugh it's a never ending Rabbit hole.

Okay well help me if you can. If not I am taking suggestions on what rabbit hole to dive down next.

I suspect that the use of symbols would become clear naturally as I gain experience with how JavaScript is "actually" used - I'm just learning it as a first language - so all I know is simple/linear JavaScript code completely isolated from interacting with any code I have not written. Either way, I'm trying to understand symbols as best I can from where I stand.

question from:https://stackoverflow.com/questions/65877355/why-are-javascript-symbol-references-safe-from-collisions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The confusion may come from how you're thinking about using symbols. I would ignore anything related to scope for this - references to Symbols will behave the same as other variables in this regard.

For your main question

"If you don’t have the reference for the Symbol, you just can’t use it"

It would be more correct to say "if you don't have * a * reference for the Symbol, you just can't use it." You don't need the original reference to the symbol, but you do need some reference to it. You can not create a new Symbol that will be referentially equal to your old symbol.

So you could have problems with collision if someone had a reference to the same Symbol, but if the reference to the symbol is not explicitly exposed, it's harder to obtain.

Consider a usecase based on the 'primary purpose' as stated in the MDN Doc on Symbols:

A symbol value may be used as an identifier for object properties; this is the data type's primary purpose, although other use-cases exist, such as enabling opaque data types, or serving as an implementation-supported unique identifier in general.

// Some method you are relying on, perhaps built by a 3rd party
const program = () => {
  // Create a symbol to use as an identifier:
  const primaryFunction = Symbol("primary");

  // This `obj` will be returned to the 'user' of the program below.
  const obj = {
    // This is harder (though not impossible) to access and
    // override from outside
    [primaryFunction]: (str) => {
      console.log("Run symbol function", str);
    },
    // This is relatively easy to access and
    // override from outside
    primaryFunction: (str) => {
      console.log("Run non-symbol function", str);
    }
  };
  
  // Examples of the two functions running as intended:
  obj[primaryFunction]("... first");
  obj.primaryFunction("... first");

  return obj;
};

// Now as a 'user' implementing `program` - get a copy of `obj` 
const myCodeUsingProgram = program();

// If I want to access the plain text key, this is trivial.
// If I weren't paying attention, I could easily override it
myCodeUsingProgram.primaryFunction = () => console.log("Easy to collide and break...");

// Now, if I want to access Symbol key, I have to do something like this.
// I have my own reference to the Symbol now...but I had to do some work to get it.
// I couldn't do something like myCodeUsingProgram[Symbol("primary")]
const newReference = Object.getOwnPropertySymbols(myCodeUsingProgram)[0];
myCodeUsingProgram[newReference] = () => console.log("Harder to collide and break...");

// Run the now 'broken' methods:
myCodeUsingProgram.primaryFunction("... second");
myCodeUsingProgram[newReference]("...second");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...