Summary of common operation methods for various reference types in JavaScript, and summary of javascript

Source: Internet
Author: User

Summary of common operation methods for various reference types in JavaScript, and summary of javascript

Object Type

Array type
Re-sorting method: compare
Ascending Order:

function compare(value1, value2){  if (value1<value2){    return -1;  }  if (value1>value2){    return 1;  } else{    return 0;  }}var values = [0,1,5,10,15];values.sort(compare);console.log(values); // [0,1,5,10,15]

Descending order:

function compare(value1, value2){  if (value1<value2){    return 1;  }  if (value1>value2){    return -1;  } else{    return 0;  }}

Slice:
Slice (start, end); slice () method returns all items starting from the specified position of the parameter to the end of the current array. If there are two parameters, this method returns an item between the starting and ending positions, but does not include an item at the ending position.

var colors = ["red", "green", "blue", "yellow", "purple"];var colors2 = colors.slice(1);var colors3 = colors.slice(1,4);console.log(colors2); // green, blue, yellow, purpleconsole.log(colors3); // green, blue, yellow

Splice:
Splice () supports deletion, insertion, and replacement.

Delete:
Two parameters are required: the location of the first item to be deleted and the number of items to be deleted.

var colors = ["red", "green", "blue"];var removed = colors.splice(0,1);console.log(colors); // greeen, blueconsole.log(removed); // red

Insert:
Three parameters are required: Start position, 0 (number of items to be deleted), and the item to be inserted.

Var colors = ["red", "green", "blue"]; var removed = colors. splice (1, 0, "yellow", "orange"); console. log (colors); // ["red", "yellow", "orange", "green", "blue"] console. log (removed); // return null

Replace:
Three parameters are required: Start position, number of items to be deleted, and any number of items to be inserted.

var colors = ["red", "green", "blue"];var removed = colors.splice(1,1,"yellow", "orange");console.log(colors); // ["red", "yellow", "orange", "blue"]console.log(removed); // ["green"]

Date type
RegExp type

var pattern1 = /[bc]/i;var pattern2 = new RegExp("[bc]at", "i");

Pattern1 and pattern2 are two fully equivalent regular expressions. Note that the two parameters passed to the RegExp constructor are strings (the RegExp constructor cannot pass the regular expression literally ). Because the pattern parameter of the RegExp constructor is a string, double escaping is required in some cases.

var pattern1 = /[bc]/i;var pattern2 = new RegExp("\\[bc\\]at", "i");

RegExp instance method
Exec

Exec receives a parameter, that is, the string to apply the pattern, and then returns an array containing the first matching information.

var text = "cat, bat, sat, fat";var pattern1 = /.at/;var matches = pattern1.exec(text);console.log(matches); // ["cat"]

Match
Match is a string method for matching regular expression rules. Its parameters are regular expressions.

var text = "cat, bat, sat, fat";var pattern1 = /.at/;var matches2 = text.match(pattern1);console.log(matches2); // ["cat"]

Test
Test () receives a string parameter

var text = "000-00-0000";var pattern = /\d{3}-\d{2}-\d{4}/;if (pattern.test(text)){  console.log("The pattern was matched"); // The pattern was matched}

Function Type
Internal Function Attributes
Convert arguments into an array

(function() {  var slice = Array.prototype.slice,    aArguments = slice.apply(arguments);    console.log(aArguments);})(10, 20, 30);arguments.callee

This property is a pointer pointing to a function that owns this arguments object. When a function runs in strict mode, access to arguments. callee may cause an error.

Function Attributes and Methods
Length
The length attribute indicates the number of name parameters that the function wants to receive.

function sayName(name){  alert(name);}function sum(num1,num2){  return num1 + num2;}function sayHi(){  alert("hi");}console.log(sayName.length); //1console.log(sum.length); //2console.log(sayHi.length); //0

Prototype

Call, apply

function sum(num1, num2){  return num1 + num2;}function callSum1(num1,num2){  return sum.apply(this,arguments);}function callSum2(num1, num2){  return sum.apply(this, [num1, num2]); }console.log(callSum1(10,10)); // 20console.log(callSum2(10,10)); //20window.color = "red";var o = {color:"blue"};function sayColor(){  console.log(this.color);}sayColor(); // redsayColor.call(this); // redsayColor.call(window); // redsayColor.call(o); // blue

Basic packaging type

var value = "25";var number = Number(value);console.log(typeof number);console.log(number instanceof Number);// falsevar obj = new Number(value);console.log(typeof obj);console.log(obj instanceof Number);// true

Boolean Type

Var falseObject = new Boolean (false); var result = falseObject & true; // true // all objects in the Boolean expression are converted to true, therefore, the falseObject object represents the trueconsole In the Boolean expression. log (result); // truevar falseValue = false; result = falseValue & true; console. log (result); // falseconsole. log (typeof falseObject); // objectconsole. log (typeof falseValue); // Booleanconsole. log (falseObject instanceof Boolean); // trueconsole. log (falseValue instanceof Boolean); // false

Number Type

var numberObject = new Number(10);var numberValue = 10;console.log(typeof numberObject); // Objectconsole.log(typoef numberValue); // numberconsole.log(numberObject instanceof Number); // trueconsole.log(numberValue instanceof Number); // false

String type
Character Method
CharAt () charCodeAt ()

The charAt () method returns the string at the given position in the form of a single character string.

CharCodeAt () returns the character encoding.

var stringValue = "hello world";console.log(stringValue.charAt(1)); // econsole.log(stringValue.charCodeAt(1)); // 101

String operation method
Concat ()

Concat () is used to concatenate one or more strings.

var stringValue = "hello ";var result = stringValue.concat("world");console.log(result); // hello worldconsole.log(stringValue); // hello

Slice (start, end)
End indicates the end of the string.
If the input is a negative number, the slice () method adds the negative value and the string length.

var str="Hello happy world!";console.log(str.slice(6)); // happy world!console.log(str.slice(6,11));// happyconsole.log(str.slice(-3)); // ld!console.log(str.slice(3, -4)); //lo happy wo 

Substring (start, end)
If the input is a negative number, substring () converts all character parameters to 0.

var str="Hello happy world!";console.log(str.substring(6)); // happy world!console.log(str.substring(6,11));// happyconsole.log(str.substring(-3)); // Hello happy world!console.log(str.substring(3, -4)); //Hel

Substr (start, length)
If the input is a negative number, the substr () method adds the length of the string to the negative first parameter, and converts the negative second parameter to 0.

Var str = "Hello world! "; Console. log (str. substr (3); // lo world! Console. log (str. substr (3, 7); // lo worlconsole. log (str. substr (-3); // ld! Console. log (str. substr (3,-3); // empty string

String position method

indexOf() lastIndexOf()var stringValue = "hello world";console.log(stringValue.indexOf("o")); // 4console.log(stringValue.lastIndexOf("o")); //7

Both methods can receive the optional second parameter, indicating the position in the string to start searching.

var stringValue = "hello world";console.log(stringValue.indexOf("o", 6)); // 7console.log(stringValue.lastIndexOf("o", 6)); //4

String Pattern Matching Method
Match ()

var text = "cat, bat, sat, fat";var pattern = /.at/;var matches = text.match(pattern);console.log(matches.index); //0console.log(matches[0]); // catconsole.log(pattern.lastIndex); //0

Search ()

var text = "cat, bat, sat, fat";var pos = text.search(/at/);console.log(pos); // 1

Replace ()

var text = "cat, bat, sat, fat";var result = text.replace("at", "ond");console.log(result); // cond, bat, sat, fatvar result = text.replace(/at/g, "ond");console.log(result); // cond, bond, sond, fond

Global Object
URI encoding method
The encodeURI () and encodeURIComponent () Methods of the Global object can encode URI (Uniform Resources Identifiers, generic resource Identifiers) for sending to the browser.

Var url = "http://www.baidu.com/"; console. log (encodeURI (url); console. log (encodeURIComponent (url); encodeURI () and encodeURIComponent () methods are respectively decodeURI () and decodeURIComponent ()

Math object
Random () method

The Math. random () method returns a random number between 0 and 1, excluding 0 and 1. For some sites, this method is very practical because it can be used to randomly display some famous sayings and news events. Using the following formula, you can use Math. random () to randomly select a value from an integer range.

Value = Math. floor (Math. random () * Total number of possible values + first possible value)

For example, if you want to select a value ranging from 1 to 10, you can write code as follows:

var num = Math.floor(Math.random()*10+1);function selectFrom(lowerValue,upperValue){  var choice = upperValue - lowerValue + 1;  return Math.floor(Math.random()*choice+lowerValue);}var num = selectFrom(2,10);console.log(num);var colors = ["red", "green", "blue", "yellow", "black", "purple", "brown"];var color = colors[selectFrom(0, colors.length-1)];console.log(color);

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.