Simple analysis of ES6 octal and binary integer literal _javascript technique

Source: Internet
Author: User
Tags file permissions

Objective

A few years ago, when SpiderMonkey implemented a rigorous model. I learned that strict mode disables the octal integer literal. Because there is evidence that some beginners will use the leading 0来 to align numbers in multiple lines, leading to unexpected results:

var sum = 015 +//equivalent to decimal 13, rather than
     197 +
     001;  Anyway it's 1
console.log (SUM)//Added and is 211, not the novice thinks 213

But there are still a number of developers who need to octal integers (especially the Mozilla extension developers and node.js developers), most commonly when it comes to processing file permissions (755,644 of these). ES6 adds a new type of octal integer literal. Similar to hexadecimal 0x or 0X, the new octal integer uses 0o or 0O as a leading indicator, followed by several octal of numeric characters (0 to 7), which is no longer bothering the novice:

var default_perms = 0o644; Also available in strict mode

It is worth noting that the 0O prefix is too readable (0 and uppercase o are too similar to distinguish), I raised the question on Esdiscuss, hoping to disable the upper-case 0O prefix, but TC39 's current decision is that consistency should be greater than readability ( Consistency refers to being consistent with 0X and 0B. I think this decision is debatable and I recommend that you never use uppercase 0O.

In addition, some developers also need binary integer literal writing, this writing ECMAScript never supported. ES6 supports this notation, which is similar to octal and hexadecimal, using a 0b or 0 B prefix:

var flt_signbit = 0b10000000000000000000000000000000;
var flt_exponent = 0b01111111100000000000000000000000;
var Flt_mantissa = 0b00000000011111111111111111111111;

Firefox Aurora has already implemented these two syntaxes, and if you are more adventurous, you can use the newer Firefox Nightly.

Summarize

The above is the entire content of this article, I hope this article's content for everyone to learn ES6 can help. If you have questions, you can exchange messages.

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.