JavaScript符号不能停止对象中的名称冲突 [英] JavaScript Symbols are not stopping name clashes in Objects

查看:78
本文介绍了JavaScript符号不能停止对象中的名称冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始研究JavaScript中的符号,并开始在我的对象中使用它们来帮助解决名称冲突,但是使用它们时我仍然可以覆盖属性吗?我很难理解JavaScript中Symbol的意义.他们谈论了很多,人们说他们很出色,因为它们不会引起Objects中的命名冲突,但是我看不到如何?

     // Create your object
let obj = {};

// Create your Symbol
let address = Symbol("the address symbol");

// Assign your Symbol as a key to a value
obj[address] = "123 Bond street";

// Return '123 Bond street'
console.log(obj[address])


// Another dev comes along

// Assigns an address property to your object
obj[address] = "456 Regent street";

// Your address property has been overwritten?
console.log(obj[address]) 

据我的代码所见,我仍然可以覆盖Object的属性吗?那么Symbol有什么帮助呢?

我用错了吗?

解决方案

似乎给人的印象是,每次访问address时,都会分配一个新的不同的Symbol值,这是不正确的.

您要做的就是使用Symbol生成一个唯一值,然后将该值存储在address变量中.这意味着以后再使用它时,您使用的是先前生成的相同符号.即使您没有使用Symbol,情况也会如此:

 let obj = {};   // Create your object
    
let address = "some key name";
obj[address] = "123 Bond street";  
console.log(obj[address]);  // Return '123 Bond street'
    
// Another dev comes along
obj[address] = "456 Regent street";   // Assigns an address property to your object
console.log(obj[address]);  // Your address property has been overwritten 

简而言之,如果您存储符号,则只不过存储了保证唯一的值而已.但是,这并不能阻止您一遍又一遍地使用它.

Symbol的更合适的用法是防止其他开发人员在与对象名称相同的现有对象上创建 new 键.其他符号:

 let obj = {};   // Create your object
    
let address = Symbol("some key name");
obj[address] = "123 Bond street";  
console.log(obj[address]);  // Return '123 Bond street'
    
// Another dev comes along
let newProp = Symbol("some key name");
obj[newProp] = "456 Regent street";   // Guaranteed unique new key is created

// There is no way the two properties would ever overwrite each other because each
// was created from a different Symbol
console.log(obj[address]);  
console.log(obj[newProp]); 

I have started to take a look at Symbols in JavaScript, and have started to use them in my Objects to help with name clashes, but I can still overwrite properties when using them? I'm having a really hard time to understand what the point of Symbols are in JavaScript. They are spoken about a lot and people say they are brilliant because they don't cause naming conflicts in Objects, but I can't see how?

    // Create your object
let obj = {};

// Create your Symbol
let address = Symbol("the address symbol");

// Assign your Symbol as a key to a value
obj[address] = "123 Bond street";

// Return '123 Bond street'
console.log(obj[address])


// Another dev comes along

// Assigns an address property to your object
obj[address] = "456 Regent street";

// Your address property has been overwritten?
console.log(obj[address])

As far as I can see from my code I can still overwrite properties on the Object? So how did the Symbol help?

Am I using them wrong?

解决方案

It appears that you are under the impression that every time you access address a new and different Symbol value will be assigned, which is not correct.

All you are doing is using Symbol to generate a unique value and then you are storing that value in the address variable. That means that when you use it again later, you are using the same Symbol you generated earlier. This would be the case even if you weren't using Symbol:

let obj = {};   // Create your object
    
let address = "some key name";
obj[address] = "123 Bond street";  
console.log(obj[address]);  // Return '123 Bond street'
    
// Another dev comes along
obj[address] = "456 Regent street";   // Assigns an address property to your object
console.log(obj[address]);  // Your address property has been overwritten

In short, if you store a Symbol, you've done nothing more than stored a value that's guaranteed to be unique. But, that doesn't stop you from using it over and over.

A more appropriate use for Symbol would be to prevent another developer from creating a new key on an existing object with the same name as keys that were made from other Symbols:

let obj = {};   // Create your object
    
let address = Symbol("some key name");
obj[address] = "123 Bond street";  
console.log(obj[address]);  // Return '123 Bond street'
    
// Another dev comes along
let newProp = Symbol("some key name");
obj[newProp] = "456 Regent street";   // Guaranteed unique new key is created

// There is no way the two properties would ever overwrite each other because each
// was created from a different Symbol
console.log(obj[address]);  
console.log(obj[newProp]);

这篇关于JavaScript符号不能停止对象中的名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆