The basis of JS Foundation---->javascript (I.)

Source: Internet
Author: User
Tags local time

Document the basics of some JavaScript. just go through a road together, why the miss to get more than after the long.

The basics of JavaScript

The instanceof operator always returns True when detecting a reference type value and an Object constructor.

var str1 = "Huhx"; var New String ("Huhx"instanceof//  falseinstanceof//  True 

There is no block-level scope in JS, the variable defined in the block is a global variable.

if (true) {    var color = "Blue"//  Blue

Third, JS in the way to create objects:

    • Use the new operator followed by the Object constructor:
var New  = "Nicholas"= 29;
    • Use object literal notation:
var person = {    "Nicholas",    +};

Four, JS in the access to the properties of the object:

var person = {    "Huhx",    "varName"var = "name"//  huhx//  HUHX

Five

The study of arrays in JS

One, there are several ways to create an array:

    • Use the Array constructor:
var New Array (); var New Array (a); var New Array ("Red", "Blue", "green");
    • Use the array literal notation:
var colors = ["Red", "Blue", "green"];

Second, the detection array:

var true  instanceof//  trueconsole.log (Array.isarray (arrays));   // true

Third, using the Join () method, you can use a different delimiter to construct the string.

// huhx,45,true // huhx|45|true

Four, the stack method of the array:

var New Array ("test"); var number = User.push ("Huhx", "Linux"//  3console.log (user);  // ["Test", "Huhx", "Linux"] var item =//  Linux//  ["Test", "HUHX"]

Push () pushes two strings into the end of the array and saves the returned result in the variable number. When a pop () is called, it returns the last item of the array.

V. Queue method for arrays:

    • The shift () method can move the first item in the array and return the item, minus 1 of the length:
var loves = ["Chenhui", "Huhx", 5]; var item = loves.shift (); Console.log (item);   // Chenhui // ["Huhx", 5]
    • The Unshift () method adds an arbitrary item to the front of the array and returns the length of the new array.
var loves = ["Chenhui", "Huhx", 5]; var true  //  5//  ["Linux", True, "Chenhui", "Huhx", 5]

Six, the reordering method of the array:

    • The reverse () method reverses the order of the array items:
var values = [1, 2, 3, 4, 5//  [5, 4, 3, 2, 1]
    • The sort () method arranges the array items in ascending order:
var values = [0, 1, 5, ten,+//  [0, 1,, 5]
    • You can pass a comparison function to the level sort method:
function Compare (value1, value2) {    return value2- value1;} var values = [0, 1, 5, ten,+// [, 5, 1, 0]

Seven, the array of some methods of operation:

    • If one or more arrays are passed to the concat () method, the method adds each item in the array to the result array.
var colors = ["Red", "green", "Blue"]; var colors2 = Colors.concat ("Yellow", ["Black", "Brown"]); Console.log (  colors) ; // ["Red", "green", "blue"] // ["Red", "green", "blue", "yellow", "black", "Brown"]
    • The slice () method can accept one or two parameters, that is, to return the starting and ending positions of an item.
var colors = ["Red", "green", "blue", "yellow", "purple"); var colors2 = colors.slice (1); var colors3 = Colors.slice (1,4//  ["Green", "blue", "yellow", "purple"]// ["Green", "blue", "yellow"]

Viii. the first parameter in splice is the location of the deletion, the second parameter is the number of deletions, followed by the inserted data and the starting point is the first parameter.

varcolors = ["Red", "green", "blue"];varremoved = Colors.splice (0, 1);//Delete First itemConsole.log (colors);//Green,blueConsole.log (removed);//Red, the returned array contains only one itemremoved = Colors.splice (1, 0, "yellow", "orange");//insert two items starting from position 1Console.log (colors);//Green,yellow,orange,blueConsole.log (removed);//an empty array is returned.removed = Colors.splice (1, 1, "Red", "purple");//insert two items to delete an itemConsole.log (colors);//Green,red,purple,orange,blueConsole.log (removed);//Yellow, the returned array contains only one item

Nine, the location of the array method:

    • IndexOf () and LastIndexOf (). Both methods receive two parameters: the subparagraphs (optional) to find indicates the index at which to find the starting point.
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];//Find the location of the 4 Console.log (Numbers.indexof (//  3 // 5

Iteration Methods for arrays:

    • Every (): Runs the given function for each item in the array, and returns True if the function returns true for each item.
    • Filter (): Each item in an array runs the given function, and returns a list of items that are true of the function.
    • ForEach (): Runs the given function for each item in the array. This method has no return value.
    • Map (): Each item in the array runs the given function, returning an array that consists of the results of each function call.
    • Some (): Runs the given function for each item in the array, and returns True if the function returns true for either item.
var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; var everyresult = Numbers.every (function(item, index, array) {    return (item > 2//  falsevar someresult = Numbers.some (function(item, Index, array) {    return (item > 2//  true

Use of the filter function:

var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; var filterresult = Numbers.filter (function(item, index, array) {    return (item > 2//  [3,4,5,4,3

Map () also returns an array, and each item of the array is the result of running the incoming function on the corresponding item in the original array.

var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; var mapresult = Numbers.map (function(item, index, array) {    return Item * 2 /c7>//  [2,4,6,8,10,8,6,4,2]

Some uses of the date in JS

First, time date creation:

var New // current time in local var New // local time May 5, 2005 PM 5:55:55

Second, the time of comparison successively and interval:

var New Date (0, 1varnew Date (1, 1//  true//  The 31-day millisecond means: 2678400000

Three, time of some value operation:

// 15 (15th) // 10 (November) // 2 (Tuesday) // 2016 (2016)

The use of regexp in JS

Each instance of REGEXP has the following properties:

0 count up. Multiline: Boolean value that indicates whether the M flag is set. Source: A string representation of a regular expression that is returned in literal form rather than in the string pattern in the incoming constructor. 

Second, the creation of RegExp objects:

var pattern1 =/[bc]at/i; var New RegExp ("[Bc]at", "I");

Third, some methods of RegExp objects:

    • EXEC () takes a parameter, which is the string to which the pattern is applied, and then returns an array containing the first occurrence information
 var  text = "Mom and Dad and baby"  var  pattern =/mom (and dad (and baby)?) /GI;  var  matches = pattern.exec (text); Console.log ( Matches.index);  //  0  Console.log (matches.input) ; //  "Mom and dad and Baby"  Console.log (Matches[0]); //  "Mom and dad and Baby"  Console.log (matches[1]); //  "and dad and Baby"  Console.log (matches[2]); //  "and baby  
    • Test (), which accepts a string parameter. Returns true if the pattern matches the parameter, otherwise returns FALSE.
var text = "000-00-0000"; var pattern =/\d{3}-\d{2}-\d{4}///  true

The use of functions in JS

The creation of a function in JS: The parser will first read the function declaration and make it available (accessible) before executing any code, and as for the function expression, it must wait until the parser executes to the line of code where it resides before it is actually interpreted.

    • function declaration Syntax Definition:
function sum (NUM1, num2) {    return num1 + num2;}
    • function expressions Define functions:
var function (NUM1, num2) {    return num1 + num2;}

Two, within the function, there are two special objects: arguments and this:

functionsum (NUM1, num2) {returnNUM1 +num2;}functionapplysum1 (NUM1, num2) {returnSum.apply ( This, arguments);//incoming Arguments Object}functionapplysum2 (NUM1, num2) {returnSum.apply ( This, [Num1, num2]);//Incoming Array}functioncallsum (NUM1, num2) {returnSum.call ( This, NUM1, num2);} Console.log (APPLYSUM1 (10, 10));// -Console.log (applysum2 (10, 10));// -Console.log (Callsum (10, 20));// -

Friendship Link

The basis of JS Foundation---->javascript (I.)

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.