最新今日头条面试题目(js闭包、原型、继承、作用域)(2016)

时间:2025-03-01 17:10:23
第一题:
已知如下类Animal,要求设计一个Cat类继承自Animal,并实现如下功能:
Animal:
function Animal(){
     = "Animal";
     = function(){
        ();
    }
}

Cat:

function Cat(){

     = "Cat";

    this.showName1 = function(){
        (); 
    }
    
    this.showName2 = function(){
        (); 
    } 
 
    this.showName3 = function(){
        (this.__super.name + "=>" + ); 
    }
}

代码运行:
// 请完善Cat部分相关代码,得到如下结果:
……..
var cat = new Cat();
(cat instanceof Animal ); // 得到:true
cat.showName1();     // 得到:"Cat" (需要读到Cat中的name属性) 
cat.showName2();    //  得到:”Animal" (需要读到Animal中的name属性) 
cat.showName3();    //得到:”Animal" => "Cat" (需要同时读到Cat中的name和Animal中的name)

答案解析:

function Animal() {
     = "Animal";
     = function() {
        ();
    };
}

function Cat() {

     = "Cat";
    this._super = ;

    this.showName1 = function() {
        ();
    };

    this.showName2 = function() {
        ();
    };

    this.showName3 = function() {
        (this._super.name + "=>" + );
    };
}
 = new Animal();
var cat = new Cat();
(cat instanceof Animal);   //true
cat.showName1();     //"Cat"
cat.();   //"Animal"
cat.showName3();    //"Animal" => "Cat"

第二题

已知道如下数组:
var arr = [[1,2,2],[3, 4, 5, 5],[6, 7, 8, 9,[11,12,[12,13,[14]]]],10];

编写一个程序将数组扁平化去并除其中重复部分数据,最终得到一个升序且不重复的数组:

var res= [1,2,3,4,5,6,7,8,9,10,11,12,13,14]


答案与解析:

/**
 * 解析数组,然后去重,排序
 * @type Array
 */

<strong>//解析数组</strong>
var arr = [[1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14]]]], 10];
var newArray = [];
function getArray(array) {
    (function(e) {
        if (typeof e === "object") {
            getArray(e);
        } else {
            (e);
        }
    });
}
getArray(arr);

<strong>//去重</strong>
 = function() {
    return (function(newArray1, newValue) {
        if ((newValue) === -1)
            (newValue);
        return newArray1;
    }, []);
};
newArray = ();
<strong>//排序</strong>
(function(a, b) {
    return a - b;
});
(newArray);


第三题:
如下题目存在哪些问题(改错)?
var obj = {
    name: " jsCoder",
    skill: ["css3","html5", "es6", "react", "angular"],
    say: function () {      
        for(var i = 0, len = ; i< len; i++){
            setTimeout(function(){
                ("No." + i + );
                ([i]);
                ('--------------------------');
            },100);
        }
    }
}
();

如何得到结果,能想到几种方法:
<span style="font-size:18px;">No.1 jsCoder
css3
--------------------------
No.2 jsCoder
html5
--------------------------
No.3 jsCoder
es6
--------------------------
No.4 jsCoder
react
--------------------------
No.5 jsCoder
angular
-------------------------- </span>

答案与解析:

<span style="font-size:18px;">/**
 * 考察this指向、闭包
 *  
 */
var obj = {
    name: " jsCoder",
    skill: ["css3", "html5", "es6", "react", "angular"],
    say: function() {
        for (var i = 0, len = ; i < len; i++) {
            (function() {
                var temp = i;
                setTimeout(function() {
                    ("No." + temp + );
                    ([temp]);
                    ('--------------------------');
                }, 100);
            })();
        }
    }
};
();</span>