zoukankan      html  css  js  c++  java
  • 继承 和 es5 新增方法 数组方法

      <script>
            // call 方法
            function fn(x, y) {
                console.log('我想喝咖啡...');
                console.log(this);
                console.log(this.name);
                console.log(x + y);

            }
            var o = {
                    name: 'andy'
                }
                // fn();
                // 1 call() 可以调用函数
                // fn.call();
                // 2 call() 可以改变 这个函数的this 指向 此时就指向了这个 o 这个对象
            fn.call(o, 1, 2);
        </script>
     
     
    <script>
            // 借用父构造函数继承属性
            // 1 父构造函数
            function Father(uname, age) {
                // this 指向父构造函数的对象实例
                this.uname = uname;
                this.age = age;
            }
            Father.prototype.monry = function() {
                    console.log(10000);

                }
                // 2 子构造函数
            function Son(uname, age, score) {
                // this 指向子构造函数的对象实例
                Father.call(this, uname, age);
                this.score = score;
            }

            Son.prototype = new Father();
            Son.prototype.constructor = Son;
            Son.prototype.exam = function() {
                console.log('孩子要考试');
            }
            var son = new Son('刘德华', 18, 100);
            console.log(son);
            console.log(Son.prototype.constructor);
        </script>
     
     

     

       // forEach 迭代(遍历)数组
            var arr = [1, 2, 3];
            var sum = 0;
            arr.forEach(function(value, index, array) {
                console.log('每个数组元素' + value);
                console.log('每个数组元素索引号' + index);
                console.log('数组本身' + array);
                sum += value;
            })
            console.log(sum);
     
     
     // filter  筛选数组
            var arr = [12, 66, 4, 88, 2, 3, 17];
            // 返回新数组
            var newArr = arr.filter(function(value, index) {
                // 返回大于等于20的条件
                // return value >= 20;
                // 返回偶数
                return value % 2 === 0;
            })
            console.log(newArr);
     
     
     
      // some 查找数组中 是否有满足条件的元素  满足就返回true 否则返回false
            var arr = [10, 30, 4];
            var flag = arr.some(function(value) {
                return value >= 20;
            })
            console.log(flag); // true

            var arr1 = ['red', 'pink', 'blue'];
            var falg1 = arr1.some(function(value) {
                return value == 'pink';
            })
            console.log(falg1); // true
     
     
  • 相关阅读:
    【html、CSS、javascript-9】jquery-选择器及过滤器
    【python之路40】Python 作用域
    H5缓存
    解决网络不可用--Using_Service_Workers
    跨域请求CORS
    基于node的websocket示例
    test
    函数节流
    ES6 promise
    web前端免费资源集
  • 原文地址:https://www.cnblogs.com/ericblog1992/p/13066703.html
Copyright © 2011-2022 走看看