Object type
Array type
Reordering Method: Compare
Ascending:
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:
function Compare (value1, value2) {
if (value1<value2) {return
1;
}
if (value1>value2) {
return-1
} else{return
0
}
}
Slice
Slice (start, end); The slice () method returns all items starting at the specified position of the parameter to the end of the current array. If there are two parameters, the method returns the item between the start and end positions, but does not include the item at the end 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, purple
console.log (COLORS3);//green, blue, yellow
Splice
Splice () has the function of removing, inserting, replacing
Delete:
requires two parameters, the position of the first item to be deleted, and the number of items to delete.
var colors = ["Red", "green", "Blue"];
var removed = Colors.splice (0,1);
Console.log (colors); Greeen, Blue
console.log (removed);//Red
Insert:
requires three parameters: Start position, 0 (number of items to delete) and items to insert
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 empty
Replace:
three parameters are required: The start position, the number of items to delete, and any number of items to insert.
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 arguments passed to the RegExp constructor are strings (you cannot pass regular expression literals to the RegExp constructor). Because the schema parameter of the RegExp constructor is a string, in some cases the string is to be double escaped.
var pattern1 =/[bc]/i;
var pattern2 = new RegExp ("\\[bc\\]at", "I");
RegExp instance method
Exec
exec receives a parameter, the string to which the pattern is applied, and then returns an array that contains 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 of string execution that matches regular expression rules, and his arguments 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 is matched");//The pattern is matched
}
function type
Function Internal Properties
convert arguments to arrays
(function () {
var slice = Array.prototype.slice,
aarguments = slice.apply (arguments);
Console.log (aarguments);
}) (a);
Arguments.callee
This property is a pointer to the function that owns the arguments object. Access to Arguments.callee can cause errors when the function is running in strict mode.
function Properties and methods
Length
The Length property represents the number of named arguments that the function expects to receive.
function Sayname (name) {
alert (name);
}
function sum (num1,num2) {return
num1 + num2;
}
function Sayhi () {
alert ("HI");
}
Console.log (sayname.length); 1
console.log (sum.length);//2
Console.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));
Console.log (callSum2 (10,10));//20
window.color = "Red";
var o = {color: "Blue"};
function Saycolor () {
console.log (this.color);
}
Saycolor (); Red
Saycolor.call (this);//Red
saycolor.call (window);//Red
saycolor.call (o);//Blue
Basic Packing Type
var value = "a";
var number = number (value);
Console.log (typeof number);
Console.log (number instanceof number);/False
var 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;
all objects in a true//Boolean expression are converted to true, so the Falseobject object represents true Console.log (Result) in a Boolean expression
;//True
var Falsevalue = false;
result = Falsevalue && true;
Console.log (result); False
Console.log (typeof falseobject);//object
Console.log (typeof falsevalue);//Boolean
Console.log (Falseobject instanceof Boolean); True
console.log (falsevalue instanceof Boolean);//False
Number Type
var numberobject = new number (a);
var numbervalue = ten;
Console.log (typeof Numberobject); Object
Console.log (Typoef numbervalue);//number
Console.log (numberobject instanceof number);//True
Console.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 a character encoding.
var stringvalue = "Hello World";
Console.log (Stringvalue.charat (1)); E
Console.log (stringvalue.charcodeat (1));//101
String manipulation methods
Concat ()
Concat () is used to stitch one or more strings together.
var stringvalue = "Hello";
var result = Stringvalue.concat ("the World");
Console.log (result); Hello World
console.log (stringvalue);//Hello
Slice (start, end)
end indicates where the string ends.
If a negative number is passed in, the slice () method adds the Passed-in negative value to the length of the string.
var str= "Hello happy world!";
Console.log (Str.slice (6)); Happy world!
Console.log (Str.slice (6,11))//Happy
Console.log (Str.slice ( -3));//ld!
Console.log (Str.slice (3,-4)); Lo Happy wo
SUBSTRING (start, end)
If a negative number is passed in, 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))//Happy
Console.log (str.substring ( -3));//Hello Happy world!
Console.log (Str.substring (3,-4)); Hel
substr (start, length)
If a negative number is passed in, the substr () method adds a negative first argument to the length of the string, and the negative second argument to 0
var str= "Hello world!";
Console.log (STR.SUBSTR (3)); Lo world!
Console.log (Str.substr (3, 7)); Lo worl
console.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")); 4
Console.log (Stringvalue.lastindexof ("O"));//7
Both methods can receive an optional second argument that indicates where to start the search from within the string.
var stringvalue = "Hello World";
Console.log (Stringvalue.indexof ("O", 6)); 7
Console.log (Stringvalue.lastindexof ("O", 6));//4
Pattern matching methods for strings
Match ()
var text = "Cat, bat, Sat, fat";
var pattern =/.at/;
var matches = Text.match (pattern);
Console.log (Matches.index); 0
Console.log (matches[0]);//Cat
Console.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, fat
var 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 the URI (uniform resources Identifiers, Universal Resource Identifier) for sending to the browser.
var url = "http://www.baidu.com/";
Console.log (encodeURI (URL));
Console.log (encodeuricomponent (URL));
The two methods of the encodeURI () and encodeURIComponent () method objects are decodeURI () and decodeURIComponent () respectively.
Math Object
Random () method
The Math.random () method returns a random number between 0 and 1, and does not contain 0 and 1. For some sites, this approach is useful because it can be used to randomly display quotes and news events. 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 code like this:
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);