zoukankan      html  css  js  c++  java
  • ECMAScript学习笔记

    1. ECMAScript不存在块级作用域,因此在循环内部定义的变量,在循环外也是可以访问的
      eg:
        var count =10;
        fpr(var i=0; i<count; i++){
          alert(i);
        }
        alert(i); //10

    2. ECMAScript的函数可以定义任意个参数,传入任意个参数两者可以不对应;
    在调用函数时传入的参数会存放在arguments[]中,定义函数时写的参数相当于为arguments[]中对应的参数起别名
    arguments[]的长度由调用函数时传入的参数决定
    在两者存在对应关系时,修改任意一个的值,另一个也会同时改变

      eg:
        function showArguments(num1,num2,num3){
          num2 = 10;
          arguments[2] = 100;
          console.log(num1+' '+num2+' '+num3+': '+arguments[0]+' '+arguments[1]+' '+arguments[2]);
        }

        showArguments(); //"undefined 10 undefined: undefined undefined 100"
        showArguments(1); //"1 10 undefined: 1 undefined 100"
        showArguments(1,1); //"1 10 undefined: 1 10 100"
        showArguments(1,1,1); //"1 10 100: 1 10 100"
        showArguments(1,1,1,1); //"1 10 100: 1 10 100"

    3. ECMAScript的所有函数的参数都是按值传递的,会把调用时参数的值付给函数内部的参数,
      (ps:当参数是基本数据类型时,不关函数如何操作都不会改变外部参数的值;
        但是当参数是引用数据类型时,因为外部参数和内部参数指向同一内存区域,
        在函数内改变对象属后,在其他地方访问该对象的也是改变后的,
        不过同样的在函数内改变参数的值指向其他地址,依然不会影响外部参数的值)
      eg:
        var obj = new Object();

        function setName(obj){
          obj.name = "Regis";
        }

        function setTitle(obj){
          obj.title = "king of lucis";
          obj = new Object();
          obj.title = "nilheim";
        }
        setName(obj);
        setTitle(obj);
        console.log(obj.name); //"Regis"
        console.log(obj.title) //"king of lucis"

    4.ECMAScript中Array类型的迭代方法
      a.every(function(item,index,array){}):对数组中的每一项运行给定函数,如果函数对每一项都返回true,则返回true。
      b.some(function(item,index,array){}):对数组中的每一项运行给定函数,如果该函数对任一项返回true,则返回true.
      c.filter(function(item,index,array){}):对数组中的每一项运行给定函数,返回该函数返回true的项组成的数组.
      d.forEach(function(item,index,array){}):对数组中的每一项运行给定的函数。该方法没有返回值
      e.map(function(item,index,array){}):对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组.

  • 相关阅读:
    LeetCode 769. Max Chunks To Make Sorted
    LeetCode 845. Longest Mountain in Array
    LeetCode 1059. All Paths from Source Lead to Destination
    1129. Shortest Path with Alternating Colors
    LeetCode 785. Is Graph Bipartite?
    LeetCode 802. Find Eventual Safe States
    LeetCode 1043. Partition Array for Maximum Sum
    LeetCode 841. Keys and Rooms
    LeetCode 1061. Lexicographically Smallest Equivalent String
    LeetCode 1102. Path With Maximum Minimum Value
  • 原文地址:https://www.cnblogs.com/LionheartCGJ/p/6117170.html
Copyright © 2011-2022 走看看