Object type
Array type
Reorder 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, the method returns the items between the start and end positions, but does not include the items at the end positions.
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() has functions of 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); // green, blueconsole.log(removed); // red
insert:
Three parameters are required: start position, 0 (number of items to be deleted), and 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 to empty
replace:
Three parameters are required: the starting position, the 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 completely equivalent regular expressions. It should be noted that both parameters passed to the RegExp constructor are strings (regex literals cannot be passed to the RegExp constructor). Since the pattern arguments of the RegExp constructor are strings, in some cases the string is double escaped.
var pattern1 = /[bc]/i;var pattern2 = new RegExp("//[bc//]at", "i");RegExp instance method
exec
exec receives a parameter, namely the string to apply the pattern, and 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 method for strings to execute matching regular expression rules, and 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
Function internal properties
Convert arguments into arrays
(function() { var slice = Array.prototype.slice, aArguments = slice.apply(arguments); console.log(aArguments);})(10, 20, 30);arguments.calleeThis property is a pointer to a function that owns this argument object. When the function is running in strict mode, accessing arguments.callee results in an error.
Function properties and methods
length
The length attribute represents the number of named parameters that the function wants to receive.
function saysName(name){ alert(name);}function sum(num1,num2){ return num1 + num2;}function saysHi(){ alert("hi");}console.log(sayName.length); //1console.log(sum.length); //2console.log(sayHi.length); //0prototype
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); // blueBasic 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 will be converted to true, so the falseObject object represents trueconsole.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 instance of 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 as a single-character string.
charCodeAt() returns 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 splice one or more strings.
var stringValue = "hello ";var result = stringValue.concat("world");console.log(result); // hello worldconsole.log(stringValue); // helloslice(start, end)
end means where the string ends.
If the passed in a negative number, the slice() method adds the passed in negative value to 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 passed in a negative number, substring() will convert all character parameters to 0
var str="Hello happy world!";console.log(str.substring(6)); // happy world!console.log(str.substring(6,11)); // happy world.log(str.substring(-3)); // Hello happy world!console.log(str.substring(3, -4)); //Hel
substr(start, length)
If the number passed in is a negative number, the substr() method adds the negative first parameter to the length of the string, 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 worldconsole.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")); //7Both methods can receive an optional second parameter indicating where to start the search in the string.
var stringValue = "hello world";console.log(stringValue.indexOf("o", 6)); // 7console.log(stringValue.lastIndexOf("o", 6)); //4Pattern matching method for strings
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, fondGlobal Objects
URI encoding method
The encodeURI() and encodeURIComponent() methods of the Global object can encode URIs (Uniform Resources Identifiers) for sending to the browser.
var url = "http://www.baidu.com/";console.log(encodeURI(url));console.log(encodeURIComponent(url));encodeURI() and encodeURIComponent() method objects are decodeURI() and decodeURIComponent()
Math object
random() method
The Math.random() method returns a random number between 0 and 1, not containing 0 and 1. This method is very practical for some sites because it can be used to randomly display some famous quotes and news events. By applying the following formula, you can use Math.random() to randomly select a value from a range of integers.
Value = Math.floor(Math.random() *Total number of possible values + first possible value)
For example: If you want to select a value between 1 and 10, you can write the code like below:
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);