ES6 Symbol类型 附带:Proxy和Set
七种数据类型
·Symbol
·undefined
·null
·Boolean
·String
·Number
·Object
let a = Symbol('this is a symbol');
let b = Symbol();
console.log(a === b); //false
Symbol的用途:在{ }作用域内可以临时重写一个变量
//
//file1.js
let name = Symbol();
{
var person = { };
person[name] = 'File1';
console.log("person[name]",person[name]);
}
//file2.js
{
let name = Symbol();
person[name] = 'File2';
//可以在局部变量{ }里临时重写,但是在全局内不变
console.log('person[name]',person[name]);
}
console.log('person[name]',person[name]);
Proxy 代理
var user = new Proxy({},{
get:function(obj,prop)
{
if(prop == 'fullname')
return obj.fname + ' ' + obj.lname;
},
/*
set:function(obj,prop){
}
*/
})
user.fname = 'Bob';
user.lname = 'Wood';
console.log("user.fullname:",user.fullname);
console.log("user.age:",user.age);
set
var arr = [1,2,3,3];
var s = new Set([1,2,3,3]);
s.add(4);
console.log("s.size",s.size);
console.log("s:",s);
s.delete(2);
console.log("s",s);
console.log("s.has(5)",s.has(5));
s.has(5);