You may not know the 10 JavaScript tips __java

Source: Internet
Author: User

Although I have been using JavaScript for development for many years, it often has some small features that surprise me. For me, JavaScript needs constant learning. In this article, I'll list 10 JavaScript usage tips, primarily for JavaScript novice and mid-level developers. Hopefully, every reader can learn at least one useful skill.

1. Variable conversion

It looks simple, but as far as I can see, using a constructor, like array () or number () for variable conversion is a common practice. Always use the original data type (sometimes called the literal amount) to convert the variable, which is more efficient without any additional impact.

var MyVar = "3.14159", str = "" + myvar,//to string int = ~~myvar,//to Integer float = 1*myvar,//to float BOOL =!! MyVar,/* to Boolean-any string with length and any number except 0 are true */array = [MyVar]; To array

The conversion date (new Date (MyVar)) and the regular expression (new RegExp (MyVar)) must use the constructor, and the form of the regular expression should be created using the/pattern/flags.

2. Decimal conversion to hexadecimal or octal, or vice versa

Do you write a separate function to convert hexadecimal (or octal)? Stop right now. There are easier out-of-the-box functions that can be used:

(int). toString (16); converts int to hex, eg => "C" (int). toString (8); converts int to octal, eg. => "parseint" (string,16)//converts hex to int, eg. "FF" => 255 parseint (string,8)//converts octal to int, eg. "=>" 16

3. Play Digital

In addition to the previous section, here are some more tips for dealing with numbers:

0xFF; Hex declaration, returns 255 020; Octal declaration, returns 1E3; Exponential, same as 1 * MATH.POW (10,3), returns 1000 (1000). toexponential (); Opposite with Previous, returns 1E3 (3.1415). toFixed (3); Rounding the number, returns "3.142"

4.Javascript Version Detection

Do you know which version of JavaScript your browser supports? If you don't know, go to Wikipedia and check out the JavaScript version of the table. For some reason, some of the features of the Javascript 1.7 version are not widely supported. However, most browsers support the features of version 1.8 and version 1.8.1. (Note: All IE browsers (IE8 or older versions) support only version 1.5 JavaScript) Here's a script that detects JavaScript versions by detecting features, and it checks for features supported by specific JavaScript versions.

var js_ver = []; (Number.prototype.toFixed)? Js_ver.push ("1.5"): false; ([].indexof && [].foreach]? Js_ver.push ("1.6"): false; ((function () {try {[a,b] = [0,1];return true;} catch (ex) {return false;}}) ())? Js_ver.push ("1.7"): false; ([].reduce && [].reduceright && JSON]? Js_ver.push ("1.8"): false; ("". Trimleft)? Js_ver.push ("1.8.1"): false; Js_ver.supports = function () {if (Arguments[0]) return (!! ~this.join (). IndexOf (Arguments[0] + ",") + ","); else return (this[this.length-1]); Alert ("Latest Javascript version supported:" + js_ver.supports ()); Alert ("Support for version 1.7:" + js_ver.supports ("1.7"));

5. Use Window.name for simple session processing

This is something I really like. You can specify a string as the value of the Window.name property until you close the label or window. Although I do not provide any scripts, I strongly recommend that you take full advantage of this method. For example, when building a Web site or application, it is useful to switch between debugging and test mode.

6. Determine whether a property exists

This problem includes two aspects, both when checking properties and getting the type of the property. But we always ignore these little things:

Bad:this would cause an error into code when Foo is undefined if (foo) {dosomething (),}//Good:this doesn ' t cause any Errors. However, even when//Foo are set to NULL or false, the condition validates as true if (typeof foo!= "undefined") {
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.