A summary of formatting methods for common types in JavaScript

Source: Internet
Author: User

Objective

I believe we all know that because JavaScript is a weak type of language, project writing will be very difficult to control, easy to all kinds of problems. Fortunately there is a strong type of typescript can make up for this defect, but the strong type provided by Typescript is compile-time, although most types-related problems can be overwritten at compile time, but there is nothing to do for a small number of errors that can go wrong at runtime.

For example, here are some common scenarios:

1, defined as the attribute of number, when assigned, if the assignment source does not have a strong type defined, this will bypass the phase of compilation check, to the runtime found that may be passed in a string. A subtraction within the class can easily lead to a large chunk of data being contaminated as Nan.

2, the definition of Boolean properties, often assigned to an object, although not obvious problems to run, but in fact, just want to save the object is empty state, it is possible that the object can never be recycled.

3, defined as an integral type of attributes, such as index. Even TS is powerless, because there is no int in TS. It is easy to pass in a floating-point number, resulting in an error when indexing from an array.

Any usage can occur while JavaScript is running, and if you want to write a strong component or framework, you have to consider these issues. The solution is to format any parameters or variables that come in from the outside, so that as soon as the format is formatted, the component and the frame can function efficiently, without any special judgment.

The following are examples of high-performance formatting methods for several common types:

Format floating point numbers

Value = +value | | 0;

Test output:

FunctionTest (value) {
 value = +value | | 0;
 returnvalue;
}
 
Test ("123");//123
Test ("123.5");//123.5
Test (123 );//123
Test (123.5);//123.5
Test ("abc");//0
Test ("123ab");//0
Test (NaN);//0
test (null);//0
Test (undefined);//0

Format signed integer (Int32)

Value = +value | 0;

Equivalent to:

value = ~~value;

Test output:

FunctionTest (value) {
 value = +value | 0;
 returnvalue;
}
 
Test ("123");//123
Test ("123.5");//123
Test (123); 123
Test (123.5);//123
Test ("-123.5");//-12 3
Test ("abc");//0
Test (NaN);//0
test (null);//0
Test (undefined);//0

Pay special attention to this: This format is only applicable to 32-bit signed integers, but also similar to the int in other languages, the number of positive integers can only be up to 2147483647 (2^31-1). UInt32 or larger int64 is not possible and will be truncated. can refer to here: the bitwise operator. In other languages, it is perfectly fine to use this format for scenarios where int is available.

Format unsigned integers (uint32)

Value = +value >>> 0;

Test output:

FunctionTest (value) {
 value = +value >>> 0;
 returnvalue;
}
 
Test ("123"); 123
Test ("123.5");//123
Test (0xFFFFFFFF);//0xfff FFFFF
Test (0xffffffff+). 5;//0xffffffff
Test ("-123. 5 ");//0xffffff85
Test (" abc ");//0
Test (NaN);//0
test (null);//0
Test (undefined);//0

Note that the displacement operator is a three-arrow >>>, and there is only this operator that operates an unsigned integer, the result is a uint32 range from 0~4294967295 (2^32-1), and all other displacement operators have the result of a signed integer (Int32), Therefore, there is no way to represent a number greater than 2147483647 (2^31-1).

Formatting Boolean values

Value =!! Value

Test output:

FunctionTest (value) {
Value =!! Value
returnvalue;
}
Test (true);//true
Test (123);//true
Test (123.5);//true
Test ({});//true
Test ([]);//true
Test ("abc");//true
Test ("");//false
Test (false);//false
Test (NaN);//false
Test (null);//false
Test (undefined);//false

format string

The format of the string is not as fixed as the requirement, and generally it is OK to avoid null, because other operations on string variables, such as the plus sign, automatically convert the type.

Value = value | | "";
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.