• 2017面试题3


    1. Array 拓展

        Array.prototype.each = function(fn) {
            try {
                // 1、目的 遍历数组每一项
                // 计数器 记录当前遍历的元素的位置
                this.i || (this.i = 0);
                // 2 严谨的判断是么时候去走each核心方法
                // 当数组的长度大于0的时候 && 传递参数必须为函数
                if (this.length > 0 && fn.constructor == Function) {
                    // 循环遍历数组每一项
                    while (this.i < this.length) {
                        // 获取数组的每一项
                        var e = this[this.i]
                        // 如果当前元素获取到了 并且当前元素是一个数组
                        if(e && e.constructor == Array){
                            // 直接做递归操作
                            e.each(fn)
                        } else {
                            // 如果不是数组 (那就是一个单个元素)
                            // 这的目的就是为了把数组的当前元素传递给fn函数 并让函数执行
                            //fn.apply(e,[e]);
                            fn.call(e,e);
                        }
                        this.i++;
                    }
                    this.i = null;
                    // 释放内存 垃圾回收机制回收变量
                }
    
            } catch (ex) {
    
            }
            return this;
        }

    2 、获取删除数组指定对象后的数组

    // 获取对象在数组下的下标
    const getIndexWithArr = function (_arr, _obj) {
      var len = _arr.length;
      for (var i = 0; i < len; i++) {
        if (_arr[i] == _obj) {
          return parseInt(i);
        }
      }
      return -1;
    };
    // 获取删除数组指定对象后的数组
    const getRemoveObjWithArr = function (_arr, _obj) {
      var length = _arr.length;
      for (var i = 0; i < length; i++) {
        if (_arr[i] == _obj) {
          if (i == 0) {
            _arr.shift(); //删除并返回数组的第一个元素
            return _arr;
          } else if (i == length - 1) {
            _arr.pop(); //删除并返回数组的最后一个元素
            return _arr;
          } else {
            _arr.splice(i, 1); //删除下标为i的元素
            return _arr;
          }
        }
      }
    };

     3、

            var a = 1;
            function aa() {
                alert(a)
            }
            aa()  // 1
    var a = 3;        
    function aa() { alert(a)
    var a ; a = 2; } aa() // undefined
    // 反例
    myname = "global"; // 全局变量
    function func() {
        alert(myname); // "undefined"
        var myname = "local";
        alert(myname); // "local"
    }
    func();

    4、

        var a = 1;
        a.b = 2;
        console.log(a + a.b) //NaN

     5、阅读下面代码该出代码在严格模式下以及非严格模式下执行后的结果

    // "use strict"
        var a = {
            a: '对象 A',
            getA: function() {
                console.log(this.a);
            }
        };
        var b = {
            a: '对象 B'
        }
        var getA = a.getA;
        var getA2 = getA.bind(a);
    
        function run(fn) {
            fn();
        }
        // 分别输出结果
        a.getA(); // 对象 A
        getA(); // Object {a: "对象 A", getA: function}
        run(a.getA); // {a: "对象 A", getA: function}
        getA2.call(b); // 对象 A
        run(getA2); // 对象 A
    // -- 添加"use strict"
    // 对象 A 报错TypeError: Cannot read property 'a' of undefined

    6、阅读下面代码,给出点击这段代码,给出点击li 3,4 打印的结果

        <ul>
            <li>0</li>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
        var nodes = document.querySelectorAll("ul li");
        for (var i = 0, len = nodes.length; i < len; i++) {
            console.log(i)
            nodes[i].addEventListener("click", function() {
                console.log("click= " + i);
            })
        }
    
        // 结果  click= 5

     7、阅读下面代码,依据打印结果给出clone 的实现方法

        function clone(obj) {
            // 实现方法
            // return obj
            var o;
            if (typeof obj == "object") {
    
                if (obj == null) {
                    o = null
                } else {
                    if (obj instanceof Array) {
                        o = [];
                        for (var i = o, len = obj.length; i < len; i++) {
                            o.push(clone(obj[i]));
                        }
                    }else {
                        o = {};
                        for(var k in obj) {
                            o[k] = clone(obj[k])
                        }
                    }
                }
    
            } else {
                o = obj;
            }
            // return o;
          return  JSON.parse(JSON.stringify(obj));
        }
    
    
    
        var a = { "name": "a" }
        var b = clone(a);
        console.log(b.name) // a
        a.name = "a1";
        console.log(b.name) // a1   a   感觉此处有问题
        b.name = 'b';
        console.log(a.name) // a1
        a.name = 'a2';
        console.log(b.name) // b

     8、

        function a(xx) {
            this.x = xx; // 等同 window.x = xx;
            console.log(this) // window
            return this;
        }
    
        // 如果互换  结果都是 window
        var x = a(5);
        var y = a(6);
        console.log(x); // 6
        console.log(y); // window
        console.log(x.x); // undefined
        console.log(y.x); // 6

    9、

        function Foo() {
    
            getName = function (){
                alert(1);
                return this;
            }
        }
    
        Foo.getName = function(){
            alert(2);
        }
    
        Foo.prototype.getName = function(){
            alert(3);
        }
    
        var getName = function(){
            alert(4);
        }
    
        function getName(){
            alert(5)
        }
    
        Foo.getName(); // 2
    
        getName(); // 4
    
        // Foo().getName(); // 报错 无返回值
    
        getName(); // 4
    
        new Foo.getName(); // 2
    
        new Foo().getName();  // 3
    
        new new Foo().getName();  // 3

     10、

        for (var i = 0, len = 3; i < len; i++) {
            setTimeout(function() {
                console.log(i) // 3
            }, 3000);
        }
    
        console.log(i + '999') // 3999
        //  3999    3  3  3
  • 相关阅读:
    java学习网址大全
    Js 提示框
    api帮助文档及常见IT学习网站
    传参给 jsp
    jsp>action
    二级横菜单显示+sitemesh母板应用
    request,session
    map>json
    清空image画布并改变大小填充背景色
    取汉字首字母方法
  • 原文地址:https://www.cnblogs.com/y896926473/p/7229408.html
Copyright © 2020-2023  润新知