A preliminary study of JavaScript function (II.)

Source: Internet
Author: User

Predefined functions for JavaScript :

The JavaScript engine has a set of built-in functions that can be called at any time.

These built-in functions include:

1. parseint ()

2. Parsefloat ()

3. IsNaN ()

4. Isfinite ()

5. encodeURI ()

6. decodeURI ()

7. encodeURIComponent ()

8. decodeURIComponent ()

9. Eval ()

=================>>>>>

  Black box function

In general, when we call a function, the program does not need to know the internal work details of the function. We can look at it as a black box, just give him some parameters, and we can get the result of her output return.

This kind of thinking can be applied to any function.

<<<<<=================

  parseint:

The w3school explanation is that the parseint () function resolves a string and returns an integer. i.e.: parseint (string, radix);

function has two invocation methods

1, the designation of Radix, this is the recommended way, unfortunately I did not do this before

2, do not specify Radix, namely: parseint (String). Although simple, but a lot of rules, this is the core of the content of this article.

parseint (string, radix)
Radix, that is, the base of the conversion, that is, we often say 2 binary, 8, 10, 16 and so on. range from 2~36, but we generally call this method in JS, the basic is the base of 10 to convert.

If the parameter is less than 2 or greater than 36, parseint () returns NaN.

The detailed rules applied by the string parameter are as follows:

1) If all are letters, return: NaN, example:

Window.onload = function Testparse () {          alert (parseint ("ABC" ) )); }

2) If it is not a letter, return: 123, Example:

Window.onload = function Testparse () {         alert (parseint ("123" ) )); }

3) If both letters and numbers are present, for example:

Window.onload = function Testparse () {         alert (parseint ("1x2bc"10 ));  // 1         Alert (parseint ("df2bc"));  // NaN}

There are two rules involved:

1) If the argument "string" starts with a number, it is converted by any number that precedes the first letter. In the example above, the first occurrence of the letter is ' X ', take the previous number string, only a number ' 1 ', the result returns 1

2) If the argument "string" starts with a letter, it returns nan directly.

The above description is explained according to ECMAScript (official translation, very strong):

The parseint () method first looks at the character at position 0, determines if it is a valid number, and if not, the method returns NaN, and no further action is taken. However, if the character is a valid number, the method will look at the character at position 1 and perform the same test. This process continues until a character is found that is not a valid number, at which point the parseint () converts the string before the character to a number.

  

  parsefloat

The parsefloat () function resolves a string and returns a floating-point number.

The function specifies whether the first character in a string is a number. If it is, the string is parsed until the end of the number is reached, and then the number is returned as a number, not as a string.

Syntax: parsefloat (string), which means the default is decimal, the other basic is the same as above

Hints and Notes

Note: only the first number is returned in a string.

Note: the opening and closing spaces are allowed.

Note: The first character of a string cannot be converted to a number, then parsefloat () returns NaN.

IsNaN

With isNaN (), we can determine whether an input value is a number that can participate in arithmetic operations. Thus the invocation of parseint () and parsefloat () can be detected successfully.

// true IsNaN (123//  falseIsNaN (1.45//  false  IsNaN ('abc123'//  true

The function will also attempt to convert its accepted arguments into numbers.

IsNaN ('123'//  falseIsNaN ('abc123'  //  true

Nan = = = Nan//False

isfinite

Isfinite () can detect whether the input is a number that is not infinity or Nan.

Isfinite (InFinity)  //  falseisfinite (-infinity)  //  false  Isfinite (+//  true//  false )

  JavaScript Coding Issues Local/Unicode

You may be a little confused about "local". At first, I don't understand, it should be the conversion between the code. Since local characters are encoded, it is reasonable to convert this encoding into Unicode encoding. In fact, Unicode encoding for all national characters is deterministic, regardless of which language you enter.

Unicode decimal and hexadecimal encodings are:

& #20363;& #23376;& #108;& #105;& #122;& #105;
& #x4f8b;& #x5b50;& #x6c;& #x69;x7a;& #x69;

The JavaScript code for the local-to-Unicode conversion is as follows:

 function Nativetounicode (str) { var     des = [];  for  (var  i = 0 ; i < str.length; I++) Des.push (  &#   + str.charcodeat (i) + "  Span style= "color: #800000;" >;  "        );  // des.push ("& #x" + str.charcodeat (    i). ToString (+) + ";");  return  des.join ( );}

The JavaScript code for Unicode to local conversion is as follows:

function unicodetonative (str) {varsrc = Str.match (/&# (\d+);g); if(src! =NULL) {        varDes = [];  for(vari =0; i < src.length; i++) Des.push (String.fromCharCode (Src[i].replace (/[&#;] /g,""))); returnDes.join (""); }    return "";}

Description

    • The String.charcodeat () method returns the Unicode encoding of the character at the specified position. This return value is an integer between 0-65535.
    • The String.fromCharCode () method can accept a specified Unicode value and then return a string.

Local/UTF-8

UTF-8 is one of the implementations of Unicode, which changes the fixed-length encoding to become longer and reduces the cost of storage and transmission. As you can see from the example below, Chinese is encoded, and there is no change to the English characters that can be represented by a single byte.

The UTF-8 code is:

& #x4F8B;& #x5B

The JavaScript code for the local conversion UTF-8 is as follows:

function navtiveToUTF8 (str) {    return str.replace (/[^\u0000-\u00ff]/g, function ($0 return Escape ($0). Replace (/(%u) (\w{4"& #x $; " ) });}

The UTF-8 translates the local JavaScript code as follows:

function utftonative (str) {    return'%u');}

Description

    • The escape () function encodes the string so that it can be read on all computers.
    • The unescape () function decodes a string encoded by escape ().

Local/ASCII

ASCII code conversion or non-conversion ASCII encoding is:

\u4f8b\u5b50lizi

\u4f8b\u5b50\u006c\u0069\u007a\u0069

The JavaScript code that converts ASCII locally is as follows:

function Nativetoascii (str, isignoreletter) {varcharacter = Str.split (""); varASCII = [];  for(vari =0; i < character.length; i++) {        varCode = number (Character[i].charcodeat (0)); if(!isignoreletter | | code >127) {            varCharascii = code.tostring ( -); Charascii=NewString ("0000"). substring (charascii.length,4) +Charascii; Ascii.push ("\\u"+charascii); }        Else{Ascii.push (character[i]); }    }    returnAscii.join ("");}

the native JavaScript code for ASCII conversion is as follows:

function asciitonative (str) {varcharacter = Str.split ("\\u"); varres = character[0];  for(vari =1; i < character.length; i++) {        varCode =Character[i]; Res+ = String.fromCharCode (parseint ("0x"+ code.substring (0,4))); if(Code.length >4) {res+ = Code.substring (4, code.length); }    }    returnRes;}

Local/URI encoding

URI encoding without the above code, it is mainly for security reasons, so that the content of the link can not see at a glance what is, to prevent malicious peek. Below is Cnblogs's "Look for" search link to encode it.

Example 4:

http://zzk.cnblogs.com/s?w= Example lizi&t=

URI encoding and component encoding are:

http://zzk.cnblogs.com/s?w=%E4%BE%8B%E5%AD%90lizi&t=

Http%3a%2f%2fzzk.cnblogs.com%2fs%3fw%3d%e4%be%8b%e5%ad%90lizi%26t%3d

Description

    • The encodeURI () function encodes a string as a URI.
    • The decodeURI () function decodes a URI encoded by the encodeURI () function.
    • The encodeURIComponent () function encodes a string as a URI component.
    • The decodeURIComponent () function decodes the URI encoded by the encodeURIComponent () function.

eval () function see next article (.? _?) /~~~

For details, see ============== "" http://www.cnblogs.com/liuning8023/archive/2012/12/09/2810518.html

A preliminary study of JavaScript function (II.)

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.