JavaScript based on the third day of clearance

Source: Internet
Author: User

JavaScript Basic clearance Third days group
    • Array reverse

      • Idea: The first one is exchanged with the last, the second is exchanged with the penultimate, and so on, the number of exchanges equals the quotient of length divided by 2.
      var array1=[10,20,30,40,50]; for(var i=0;i<array1.length/2;i++){     var temp=array1[i];     array1[i]=array1[array1.length-1-i];     array1[array1.length-1-i]=temp; } console.log(array1);
    • Bubble sort

      • Idea: Exchange ORDER by 22 comparison
      • Icon:
      • Comparison of rounds: Length-1
      • Number of comparisons per round: length of wheel

        var array=[10,0,30,60,35];  for(var i=0;i<array.length-1;i++){//轮数      for(var j=0;j<array.length-1-i;j++){//每一轮比较的次数          if(array[j]>array[j+1]){//从小到大的顺序              var temp=array[j];              array[j]=array[j+1];              array[j+1]=temp;          }      }  }  console.log(array);
Function
    • Arguments use of pseudo-arrays
function f1(){    //好处,不用再定义形参从而限制形参个数    var sum=0;    for(var i=0;i<arguments.length;i++){        sum +=arguments[i];    }    return sum;}console.log(f1(10,20,30));//60
    • How to define
    1. function expression
var f2=function(){//将一个匿名函数给了一个变量    console.log("这是函数表达式");};//这是一个赋值过程,必须加分号
    1. Function-Declarative

      function f2(){    console.log("这是函数声明式");}
    2. Difference

      function f1(){    console.log("1");}f1();function f1(){    console.log("2");}//只有函数声明式会提前声明f1();

      Equivalent

      function f1(){    console.log("1");}function f1(){    console.log("2");}f1();f1();

      Two call results will output 2

      f1=function(){    console.log("1");}f1();f1=function(){    console.log("2");//函数表达式不会提前声明,所以按照顺序输出}f1();

      Output 1 First, then output 2

    • function as a parameter
function f1(fn){    fn();}function f2(){    console.log("这是函数作为参数的实例");}f1(f2);//注意这里传入函数名称,而不是函数的返回值(f2())
  • function as return value

      function F2 () {return function F3 () {Consol      E.log ("function as return value"); }}var F1=f2 ();//f1 received a function F1 ();//Call F1  
  • Pre-parsing

    The declaration of variables and functions precedes the caller of the current scope in advance ( assignment is not advanced )

    f1();function f1(){    console.log(num1);    var num1=10;}

    Output: undefined

    If "var num1=10" is removed, an error will be made (not stated)

    Equivalent:

    function f1(){    var num1;    console.log(num1);}f1();
    f1();var num1=20;function f1(){    console.log(num1);    var num1=10;}//在f1内部也提前了变量声明,同样没有提前变量赋值

    Output: undefined

    Equivalent:

    var num1=20;function f1(){    var num1;    console.log(num1);}f1();
    f1();var num1=20;function f1(){    console.log(num1);}//变量声明(num1)和函数声明(f1)提前,但是变量赋值没有提前

    Output: undefined

var num1=20;function f1(){    console.log(num1);}f1();//不存在提前,顺序调用

? Output: 20

    • Study Questions

      f1();console.log(a);console.log(b);console.log(c);function f1(){    var a=b=c=9;    console.log(a);  console.log(b);  console.log(c);}

      Thinking......

Results:

Above: A error b=9 c=9

Below: a=9 b=9 c=9

Equivalent:

function f1(){    var a=9;    b=9;//b与c前面没有var,所以他们是隐式全局变量    c=9;    console.log(a);//9    console.log(b);//9    console.log(c);//9}f1();console.log(a);//报错(a为局部变量)console.log(b);//9console.log(c);//9

JavaScript based on the third day of clearance

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.