首页 > 编程语言 > 详细

JavaScript(ES5)使用保留字作函数名

时间:2016-04-19 19:33:52      阅读:181      评论:0      收藏:0      [点我收藏+]

ES5同意直接使用保留字作为属性名。但却不同意直接使用保留字作为函数名

设现有类NSMap,若要给NSMap的原型加delete方法,如

function NSMap(){

}
NSMap.prototype.delete=function delete(){

};

则浏览器解析报错

SyntaxError: Unexpected token delete

那么,为什么native code的Map能够办到?




后来想到标识符能够由除ASCII特殊字符以外的大部分Unicode字符组成,方案来了:

//1.从保留字中随便挑个字符出来,如字符t,算出字符t的十六进制charCode。

"t".charCodeAt(0).toString(16)//"74"

//2.尝试将t用\x74表示。使用dele\x74e作为函数名,不行

NSMap.prototype.delete=function dele\x74e(){
	
};

//3.尝试将t用\u0074表示,使用dele\u0074e作为函数名,总算不报错了

NSMap.prototype.delete=function dele\u0074e(){
	
};

//(这里有点不解。\x74和\u0074不是同一个意思吗,有可能是因解析器的性能考虑而不支持\x)

//4.假设函数是独立声明的,引用函数也不得直接使用字面保留字

function dele\u0074e(){
	
}
NSMap.prototype.delete=dele\u0074e;

//用得较多时,能够參照Chrome底层JavaScript源代码那样写

InstallFunctions(NSMap.prototype,DONT_ENUM,[
	"extends",function ex\u0074ends(){
		
	},
	"delete",function dele\u0074e(){
		
	}
]);




另附上一个转义函数

function toCharCodeString(){
	return Array.prototype.map.call(new String(this),function(c){
		var code=c.charCodeAt(0),
			hex=code.toString(16);
		//return code>0xff?

// "\\u"+"000".substr(0,4-hex.length)+hex: // "\\x"+"0".substr(0,2-hex.length)+hex; return "\\u"+"000".substr(0,4-hex.length)+hex; }).join(""); } toCharCodeString.call("delete"); // "\u0064\u0065\u006c\u0065\u0074\u0065" toCharCodeString.call("Unicode字符"); // "\u0055\u006e\u0069\u0063\u006f\u0064\u0065\u5b57\u7b26"




JavaScript(ES5)使用保留字作函数名

原文:http://www.cnblogs.com/bhlsheji/p/5409231.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!