JavaScript (ES5) uses reserved words for function names

Source: Internet
Author: User

ES5 agrees to use reserved words directly as property names. But do not agree to use reserved words directly as function names

Set the existing class Nsmap to add the Delete method to the Nsmap prototype, such as

function Nsmap () {}nsmap.prototype.delete=function Delete () {};

The browser resolves the error

syntaxerror:unexpected token Delete

So why is native code map able to do that?



It was later thought that the identifier could consist of most of the Unicode characters except the ASCII special characters, and the scheme came:

1. Choose a character from the reserved word, such as the character T, to figure out the hexadecimal charcode of the character T.

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

2. Try to represent T with \x74. Use dele\x74e as the function name, no

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

3. Try to express T with \u0074, using dele\u0074e as the function name, and finally no error

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

(A little puzzled here.) \x74 and \u0074 not the same, it's possible that the parser's performance considerations do not support \x)

4. Assuming that the function is declared independently, the reference function must not directly use literal reserved words

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

When used more, be able to write in the same way as Chrome's underlying JavaScript source code

Installfunctions (nsmap.prototype,dont_enum,["extends", function Ex\u0074ends () {}, "delete", function dele\u0074e () { }]);



Attach the previous escape function

function tocharcodestring () {return Array.prototype.map.call (new String (This), function (c) {var code=c.charcodeat (0), Hex=code.tostring (+);//return code>0xff?

"\\u" + "substr" (0,4-hex.length) +hex://"\\x" + "0". substr (0,2-hex.length) +hex;return "\\u" + "$". substr (0,4- hex.length) +hex;}). Join ("");} Tocharcodestring.call ("delete"); "\u0064\u0065\u006c\u0065\u0074\u0065" Tocharcodestring.call ("Unicode character"); "\u0055\u006e\u0069\u0063\u006f\u0064\u0065\u5b57\u7b26"




JavaScript (ES5) uses reserved words for function names

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.