let s1 = Symbol("foo")
let s2 = Symbol("bar")
console.log(s1,s2)
const obj = {
toString(){
return 'abc'
}
}
const sym = Symbol(obj)
console.log(sym)
console.log(String(sym),sym.toString())
let s3 = Symbol()
console.log(Boolean(!s3))
console.log(s1.description)
let mySymbol = Symbol()
let test = Symbol()
let a = {
[mySymbol]:'Hello!',
[test](arg){
console.log('test',arg)
}
}
console.log(a[mySymbol])
a[test](222)
const COLOR_RED = Symbol('COLOR_RED');
const COLOR_GREEN = Symbol('COLOR_GREEN');
function getComplement(color) {
switch (color) {
case COLOR_RED:
return COLOR_GREEN;
case COLOR_GREEN:
return COLOR_RED;
default:
throw new Error('Undefined color');
}
}
console.log(getComplement(COLOR_RED))
const shapeType = {
triangle: 'Triangle'
};
function getArea(shape, options) {
let area = 0;
switch (shape) {
case shapeType.triangle:
area = .5 * options.width * options.height;
break;
}
return area;
}
console.log(getArea(shapeType.triangle,
{ width: 100, height: 100 }));