functiontest(){
console.log(123);}test();// 123const another = test;another();// 123
test =null;another();// 123
ES6 中所有的函数对象都暴露了一个只读属性 name
console.log(test.name);// test
console.log((()=>{}).name);// ""
console.log(newFunction().name);// anonymous
获取函数、设置函数或者使用 bind()实例化,名称前会有前缀
functionfoo(){}
console.log(foo.bind(null).name);// bound foolet dog ={getage(){}};const pd = Object.getOwnPropertyDescriptor(dog,"age");
console.log(pd.get.name);// get age
functionPerson(){let name ='jiy';// 特权方法this.setName=function(value){
name = value;}// 特权方法this.getName=function(){return name;}}const p =newPerson();
console.log(p.name);// undefined
console.log(p.getName());// jiy
10.16.1 静态私有变量
(function(){// name 由 Person 的所有实例共享let name ="jiy";// 不使用关键字,使变量创建到全局作用域上Person=function(){};
Person.prototype.getName=function(){return name;};})();
10.16.2 模块模式
letsingle=function(){// 私有变量和函数let name ="jiy";// 特权/公有方法和属性return{
type:"person",getName(){return name;},setName(value){
name = value;}}}();
10.16.3 模块增强模式
letsingle=function(){let name ="jiy";// 可以指定单例为特定类型的实例let obj =newCustomType();
obj.type ="person";
obj.getName=function(){return name;};
obj.setName=function(value){
name = value;};return obj;}();