最新今日头条面试题目(js闭包、原型、继承、作用域)(2016)
2016-09-30 11:12
691 查看
第一题:
已知如下类Animal,要求设计一个Cat类继承自Animal,并实现如下功能:
代码运行:
// 请完善Cat部分相关代码,得到如下结果:
……..
答案解析:
function Animal() {
this.name = "Animal";
this.showName = function() {
console.log(this.name);
};
}
function Cat() {
this.name = "Cat";
this._super = Cat.prototype;
this.showName1 = function() {
console.log(this.name);
};
this.showName2 = function() {
console.log(this.name);
};
this.showName3 = function() {
console.log(this._super.name + "=>" + this.name);
};
}
Cat.prototype = new Animal();
var cat = new Cat();
console.log(cat instanceof Animal); //true
cat.showName1(); //"Cat"
cat.showName2.call(Cat.prototype); //"Animal"
cat.showName3(); //"Animal" => "Cat"
第二题:
已知道如下数组:
编写一个程序将数组扁平化去并除其中重复部分数据,最终得到一个升序且不重复的数组:
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) {
array.forEach(function(e) {
if (typeof e === "object") {
getArray(e);
} else {
newArray.push(e);
}
});
}
getArray(arr);
<strong>//去重</strong>
Array.prototype.distinct = function() {
return this.reduce(function(newArray1, newValue) {
if (newArray1.indexOf(newValue) === -1)
newArray1.push(newValue);
return newArray1;
}, []);
};
newArray = newArray.distinct();
<strong>//排序</strong>
newArray.sort(function(a, b) {
return a - b;
});
console.log(newArray);
第三题:
如下题目存在哪些问题(改错)?
如何得到结果,能想到几种方法:
答案与解析:
<span style="font-size:18px;">/**
* 考察this指向、闭包
*
*/
var obj = {
name: " jsCoder",
skill: ["css3", "html5", "es6", "react", "angular"],
say: function() {
for (var i = 0, len = this.skill.length; i < len; i++) {
(function() {
var temp = i;
setTimeout(function() {
console.log("No." + temp + obj.name);
console.log(obj.skill[temp]);
console.log('--------------------------');
}, 100);
})();
}
}
};
obj.say();</span>
已知如下类Animal,要求设计一个Cat类继承自Animal,并实现如下功能:
Animal: function Animal(){ this.name = "Animal"; this.showName = function(){ console.log(this.name); } } Cat: function Cat(){ this.name = "Cat"; this.showName1 = function(){ console.log(this.name); } this.showName2 = function(){ console.log(this.name); } this.showName3 = function(){ console.log(this.__super.name + "=>" + this.name); } }
代码运行:
// 请完善Cat部分相关代码,得到如下结果:
……..
var cat = new Cat(); console.log(cat instanceof Animal ); // 得到:true cat.showName1(); // 得到:"Cat" (需要读到Cat中的name属性) cat.showName2(); // 得到:”Animal" (需要读到Animal中的name属性) cat.showName3(); //得到:”Animal" => "Cat" (需要同时读到Cat中的name和Animal中的name)
答案解析:
function Animal() {
this.name = "Animal";
this.showName = function() {
console.log(this.name);
};
}
function Cat() {
this.name = "Cat";
this._super = Cat.prototype;
this.showName1 = function() {
console.log(this.name);
};
this.showName2 = function() {
console.log(this.name);
};
this.showName3 = function() {
console.log(this._super.name + "=>" + this.name);
};
}
Cat.prototype = new Animal();
var cat = new Cat();
console.log(cat instanceof Animal); //true
cat.showName1(); //"Cat"
cat.showName2.call(Cat.prototype); //"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) {
array.forEach(function(e) {
if (typeof e === "object") {
getArray(e);
} else {
newArray.push(e);
}
});
}
getArray(arr);
<strong>//去重</strong>
Array.prototype.distinct = function() {
return this.reduce(function(newArray1, newValue) {
if (newArray1.indexOf(newValue) === -1)
newArray1.push(newValue);
return newArray1;
}, []);
};
newArray = newArray.distinct();
<strong>//排序</strong>
newArray.sort(function(a, b) {
return a - b;
});
console.log(newArray);
第三题:
如下题目存在哪些问题(改错)?
var obj = { name: " jsCoder", skill: ["css3","html5", "es6", "react", "angular"], say: function () { for(var i = 0, len = this.skill.length; i< len; i++){ setTimeout(function(){ console.log("No." + i + this.name); console.log(this.skill[i]); console.log('--------------------------'); },100); } } } obj.say();
如何得到结果,能想到几种方法:
<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 = this.skill.length; i < len; i++) {
(function() {
var temp = i;
setTimeout(function() {
console.log("No." + temp + obj.name);
console.log(obj.skill[temp]);
console.log('--------------------------');
}, 100);
})();
}
}
};
obj.say();</span>
相关文章推荐
- JS变量提升、匿名函数、原型继承、作用域、闭包机制等。
- 变量对象,作用域链,闭包,匿名函数,this关键字,原型链,构造器,js预编译,对象模型,执行模型,prototype继承
- js重点浅谈(跨域,作用域和作用域链,闭包,原型和原型链继承)
- ACM--面试--回文解码--今日头条2017实习offer题目
- css作用域的理解--从刷今日头条遇到的js题目说起
- JavaScript高级内容笔记:原型链、继承、执行上下文、作用域链、闭包
- js闭包面试题目
- 一切皆对象之两个方法概括js,无函数签名(无多态),原型,闭包,封装,引用类型,继承……
- JavaScript高级内容笔记:原型链、继承、执行上下文、作用域链、闭包
- 作用域、上下文、闭包、原型、继承
- JS 面试知识学习历程(第三天) -- 作用域和闭包
- js系列教程13-原型、原型链、作用链、闭包全解
- 今日头条面试总结——闭包和this
- 一道题目理解js中变量在作用域链与原型链中的查找顺序
- 前端开发必须知道的JS之原型和继承
- JavaScript全面了解作用域(基础、this、闭包、继承)之一
- 一个 JS 面试题目
- jvaScript很好的教程,原型,继承,闭包,面向对象,全局变量,推荐
- JavaScript全面了解作用域(基础、this、闭包、继承)之二
- JS闭包之词法作用域