JS Basic Four

Source: Internet
Author: User
Tags array sort

Scope

Variables defined inside the function designed to be used internally by the function

Function-level scope in JS, only the function will produce scope, and if, whhile, for will not produce scope

Example var a =1; var fn1 = function () {

var a = 10;

}; var fn2 = function () {

Console.log (a);   The result here is 1.

}; fn1 (); FN2 ();

Example var a = ten;

function fn (b) {System automatically declares B:var B = 10;

A =30;function fn1 () {var a = 40;console.log (a);   The printed result is 40}function fn2 () {a = 50; assigns a value to a, where fn2 is not defined console.log (a); the printed result is 50}fn1 ();   The meaning of this is to call the function fn1fn2 ();   The meaning of this is to call the function Fn2console.log (a);   print result is 50

}fn (a); Console.log (a); Printing results are

The subsequent need to use the function of the results of the calculation need to use the return value returned, hope that the function of the calculation results, the subsequent need to use the addition, do not need to add

Variable promotion: When declaring a variable, the time at which the current variable is declared will be placed at the beginning of the current scope

  Only the declaration of the variable is promoted, and the assignment of the variable is not promoted

Cosole.log (a);var a = 10; Here is shorthand, one declares var a, a = 10, assigns 10 to a

Meaning of variable promotion:var A; Console.log (a); Here the print result is not defined undefined a = ten; A's assignment of 10 does not improve, so the above print result is undefined

objects are enclosed in curly braces and are unordered collections.

var obj = {

Name: ' Bob ',    between the data and the data, separated by     this name as the property name, followed by Bob; The property name can be added ", such as ' name ' = ' Bob ', without adding or affecting age:20,gender: ' Male ' 123:flase ,      the property name here can start with a number, but ' test+test ': ' Hello ',         here you can use +, but if the property name is not used here, it will go wrong.  

}Console.log (obj);

Console.log (Obj.name) takes the value of the object property by one: Obj's object Name property, Dot:. The point will take the name immediately after the dot as the property name. The value that follows is taken as a property,

Console.log (obj[' name ')) mode two: Add square brackets: The return value in [] as the property name //Console.log (obj.test+test) Prints the result as Unfi Use point: For some special property names, such as the beginning of a number, or some symbol in the middle of the property name, these properties can only be used by square brackets to fetch Console.loe (obj[' 123 '). The code will be compact, more intuitive, and recommended. To fetch property values

function fn (a,b,c) {

var arr = [A,b,c];for (var i = 0;i < arr.length-1;i++) {   array sort for (var j = 0; j< arr.length-i-1;j++) {   ex length- The largest of the I times is placed on the last side if (Arr[j] > arr [(j + 1]) {var temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}                                 

}

var arr = [Console.log](arr[' 0 ')) here 0, add no ' ' Print the result is the same, for arrays, the object can use the format, array also can. []Console.log (arr[' length ')) here prints the number of items in this array, 3 items, and the array comes with an object that is a set of arrays.

Examples of object and array differences:var obj = {

0:1,1:2,2:3,

}Console.log (obj) console.log (obj[' length ') This is an object that does not print the number of items in the array, and the changes to the properties need to be defined to have

Array object methods to   build arrays of the following three ways      var arr = [three-way] This is the array literal, if there is no special case, this way, it is more convenient than the following three methods

One: Condytuvyot is handling inherited var when arr = new Array () assignment is not intuitive arr[0] =1; ARR[1] = 2; arr[2] =3;

Two: thevar arr = new Array (5) does not store the data, length is 5, if arr[5], the printout is 6

III: var arr = new Array (three-by-four)

How to use the array join var arr = [I/n, ' hello ']; var result = Arr.join (); Each item of the array is stitched together console.log (arr);console.log (Result) ;

Array Pop use method: The last item of the array is deleted, the array itself will be modified, the deletion of an item as the return value of the method if the last item, with [] value can be var arr = [three-way, ' hello ']; var result = Arr.pop ();Console.log (arr);console.log (result);

Array of pus usage: add one or more new items to the array, the first item after the number of items can be written or not to write, will modify the array itself and return the new length, add the new item is added to the end, if it is to add an array, the array as an item (preserving the array), add to the end, example 2 var arr = [A/n, ' hello ']; var result = Arr.pus (' world ');console.log (arr);console.log (result); Example 2

var arr = [];var, ' hello ' arr2 = [Nh,nk]var result = Arr.pus (' arr2 '); Console.log (arr); Console.log (result);

Reverse method: Modify the element group, the left-to-right of the array, and change to the right-to-left Shift method: Delete the first item of the array and delete an item as the return value of the method

Slice method, need to fill in the beginning of the corner mark, and the end of the corner mark (the end of the corner label 5 is not included, will not be selected) The corner mark can be negative, only slice here can use negative numbers, representing the right-to-left object to intercept the array of fragments will not modify the array itself, intercept the length of the actual var arr = [4,5, ' hello ', ' js '];

var result = Arr.slice (' 3,5 ');    var result = Arr.slice (' 3,-2 '); Console.log (arr); Console.log (result);

Sort method: Sorts the elements of an array automatically from small to large, sorting the first character from left to right (to understand the mechanism of bubble sort nesting loops, how to sort the array from small to large, important!) ) var arr = [2,1,3,7,4]; var arr = [23,1,300,7,4]; The subsequent print result is 1,23,300,4,7

Arr.sort (); Console.log (arr);    The print result specifies the sort sorting comparison rule for 1,2,3,4,7, in a function that functions rule (A, b) {    if (a < b) {Compares A,      a and which is in front         if you want a to appear in front of B to return a number less than 0 return-1;}else{to make B appear in front of a, returns a number greater than 0, return 1;}

}arr.sort (rule); Arr.sort (rule ()), after which this represents the return value after the call, evaluates the argument first, prints it, or console.log (arr) , which is passed in by default collation

The above abbreviations   from small to large arranged     from the large to the smallest line will return a-B to return b-afunction rule (A, B) {return a-b;}arr.sort (rule); Console.log (AR R);

Splice method: function One. Delete a part of the middle of the array needs to fill two parameters, a number of deleted corners, the second parameter write need to delete a few items

var arr = [23,1,300,7,4]var result = Arr.splice (2,1) Console.log (arr); Console.log (result); This is the return value of the deleted item   two. Replace the attribute, substituting a portion of the array    var arr = [23,1,300,7,4]var result = Arr.splice (2,1,400,500) Console.log (arr); Console.log (Result ) Effect three. Insert a new item somewhere in the array var arr = [23,1,300,7,4]var result = Arr.splice (3,0,100,200)   3 means insert in the position of the corner Mark 3, 0 means that the item in the array is not deleted Console.log ( ARR); Console.log (Result)

ToString Method: Converts an array into a string and returns the result shift deletes and returns the first element of the number unshift adds a new element to the beginning of the array

JS Basic Four

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.