Standard functions commonly used in JS

Source: Internet
Author: User
Tags hasownproperty

Original: JS common standard function

1. Array type function Array.concat (item ...)

function function: Associative array, implement array addition function, but do not affect original array, concat return new array.

Array.join (separator)

function function: Each element in the array is constructed into a string with separator as a delimiter, the default separator is a comma, and of course you can specify it yourself separator

In fact, the most common place in the Join method is added in the string, with JS people know that JS string addition is particularly slow and poor performance, put a large number of fragment string in an array and join method to connect than with + connection is much faster.

Array.pop ()

The pop and push methods make array arrays work like stacks.

function function: The Pop method removes the last element in the array and returns the element.

If the array is empty, the undefined is returned;

Array.push (item ...)

function function: The push method attaches one or more item parameters to the end of an array. But unlike the concat method, the push method does not modify the item parameter, and if the parameter item is an array, it adds the parameter array to the array as a single element, returning the new length of the array

Array.reverse ()

function function: Reverses the order of the elements in the array, returns the current array

Array.shift ()

function function: Removes the first element in the array and returns the element, which returns undefined if the array is empty.

Shift is much slower than pop

Array.slice (start,end)

function function: Make a shallow copy of the paragraph in the array. The end parameter is optional, and the default value is array.length for the length of the array.

If any of the two parameters are negative, Array.Length will add them to try to make them non-negative.

If start is greater than or equal to array.length, the resulting result will be a new empty array

Array.Sort (COMPAREFN)

function function: Sorts the contents of an array.

        var n = [4, 8, +-----];        N.sort (function  (A, b) {            return A- b;        });         //
View Code

        varm = [' AA ', ' BB ', ' a ', 4, 8, 15, 16, 23, 43]; M.sort (function(A, b) {if(A = = =b) {return0; }            if(typeofA = = =typeofb) {returnA < b? -1:1; }            return typeofA <typeofB? -1:1;        }); Console.log (m); //[4, 8, A, a, a, "AA", "BB"]
View Code

        varCompare =function(name) {return function(o, p) {varA, B; if(typeofo = = = ' object ' &&typeofp = = = ' object ' && o &&p) {a=O[name]; b=P[name]; if(A = = =b) {return0; }                    if(typeofA = = =typeofb) {returnA < b? -1:1; }                    return typeofA <typeofB? -1:1; } Else {                    Throw{name:"Error", message:' Expected an object when sorting by ' +name};        }            };        }; vars =[{First:' Joe ', Last: ' Felix '}, {first:' Moe ', Last: ' Besser '}, {first:' Joe ', Last: ' Howard '}, {first:' Shemp ', Last: ' DeRita '}, {first:' Larry ', Last: ' Howard '}, {first:' Curly ', Last: ' Fine '}, {first:' Shirly ', Last: ' Angela ' }        ]; S.sort (Compare (' First ')); /*s = [{first: ' Curly ', Last: ' Fine '}, {first: ' Joe ', Last: ' Felix '}, {first: ' Joe ', Last: ' Howard ', {first: ' Larry ', Last: ' Howard '}, {first: ' Moe ', Last: ' Besser '}, {first: ' S        Hemp ', Last: ' DeRita '}, {first: ' Shirly ', Last: ' Angela '}]; */
View Code

Array.splice (Start,deletecount,item ...)

function function: Remove 1 or more elements from the array and replace them with a new item. Returns an array containing the elements to remove.

Array.unshift (item ...)

function function: Inserts the item at the beginning of the array. Returns the new length value of the array.

2. Function type functions function.apply (Thisarg,argarray)

Function.prototype.bind =function(that) {//returns a function that invokes the function as if it were the method of that object            varMETHOD = This, Slice=Array.prototype.slice, args= slice.apply (arguments, [1]); return function () {                returnMethod.apply (That, Args.concat (slice.apply (arguments, [0])));        };        }; varx =function () {            return  This. Value; }. Bind ({value:666 }); Console.log (x ());//666
View Code

3. Number type function number.toexponential (fractiondigits)

function Function: Converts number to a string in exponential form. The optional parameter fractiondigits controls the number of digits after the decimal point, and its value must be between 0 and 20.

number.tofixed (fractiondigits)

function Function: Converts number to a string in decimal form. The optional parameter fractiondigits controls the number of digits after the decimal point, and its value must be between 0 and 20. The default is 0.

number.toprecision (Precision)

function Function: Converts number to a string in decimal form. The optional parameter fractiondigits controls the number of digits after the decimal point, and its value must be between 1 and 21.

number.tostring (Radix)

function Function: Converts number to a string. Reliable parameter radix control cardinality, its value must be between 2 and 36, the default is ten

4. Object type function object.hasownproperty (name)

function Function: If object contains a property named name, the hasOwnProperty method returns True, and the property with the same name in the prototype chain will not be checked.

         for inch obj) {            if  (Obj.hasownproperty (name)) {                console.log (obj[name]);            }        }

5. regexp type function regexp.exec (String)

function function: Successfully matches regexp and string, returns an array.

The element labeled 0 in the array contains a substring that matches the regular expression regexp

The element labeled 1 is the text captured in group 1, the element labeled 2 is the text captured by grouping 2, and so on. If the match fails, it returns NULL.

If RegExp has a G (global) flag, the lookup does not start at the starting position of the string,  Instead, start with the Regexp.lastindex (its initial value is 0), and if the match succeeds, then Regexp.lastindex will be set to the position of the first character after the match. Unsuccessful match resets Regexp.lastindex to 0

regexp.test (String)

function Function: Returns True if RegExp matches string, false otherwise

6. String type functionString.charat (POS)

function function: Returns the character at the POS position in string.

If POS is less than 0 or greater than String.Length, an empty string is returned

string.charcodeat (POS)

function function: Returns the character encoding of the character at the POS position in string.

String.Concat (String ...)

function Function: Connection string (however seldom used, because this function can also be implemented with operator +)

String.IndexOf (searchstring,position)

function function: Finds another string within string searchstring, returns the position of the first matching character if found, otherwise returns-1.

The optional parameter position can be set to start a search from a position in string.

String.LastIndexOf (searchstring,position)

function function: Similar to the IndexOf method, the difference is to start looking at the end of the string instead of from the beginning.

String.localcompare (that)

function function: Comparison of two strings

String.match (regexp)

function function: matches a string and a regular expression.

If there is no G-ID,string.match (regexp) and regexp.exec (string) have the same result.

If there is a G ID, returns an array containing all matches except for the capturing group.

string.replace (Searchvalue,replacevalue)

function function: Finds and replaces a string and returns a new string.

        var oldareacode =/\ ((\d{3}) \)/g;         var p = ' (555) (666) -1212 '. Replace (Oldareacode, ' $1-');        Console.log (p); // 555-666--1212

String.search (regexp)

function function: Similar to the IndexOf method, except that it accepts a regular expression as a parameter instead of a string, if a match is found to return the first match of the first character position, or 1 if no match is found.

String.slice (start,end)

function Function: Copies a part of a string to construct a new string.

If the start parameter is a negative number, it is added to the string.length. The end parameter is optional, and its default value is String.Length. If the end parameter is a negative number, it is added to the string.length. The end parameter is a number larger than the position value of the last character.

string.split (separator,limit)

function function: Splits a string into fragments to create an array of strings. The optional parameter limit limits the number of fragments to be split.

        var digits = ' 0123456789 ';         var //

string.substring (start,end)

function function: The use of substring is the same as the slice method, except that it cannot handle negative parameters, and generally replaces it with slice.

string.tolocallowercase ()

function Function: Converts all letters in a string to lowercase format with localized rules.

string.tolocaluppercase ()

function Function: Converts all letters in string to uppercase format with localized rules.

string.tolowercase ()

function function: converts all letters in a string to lowercase format.

string.touppercase ()

function function: converts all the letters in a string to a large format.

String.fromCharCode (char ...)

function Function: Returns a string from a list of numbers.

        var a = String.fromCharCode (for the duration of the ();        Console.log (a); // Cat;

Standard functions commonly used in JS

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.