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

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"



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章