2020年出现的大厂面试题
面试题1:作用域链
var x
= 1;
function func(x
, y = function anonymous1() {x
= 2}) {
x
= 3;
y();
console
.log(x
);
}
func(5);
console
.log(x
);
1.函数func作用域内的变量是私有的,形参赋值x=5,私有赋值x=3,anonymous1函数的作用域链是func,所以函数y()执行会改变x=2,第一个console.log(x)打印2 2.全局作用域,第二个打印数值 x=1
面试题2:作用域
var x
= 1;
function func(x
, y = function anonymous1() {x
= 2}) {
var x
= 3;
y();
console
.log(x
);
}
func(5);
console
.log(x
);
var x
= 1;
function func(x
, y = function anonymous1() {x
= 2}) {
var x
= 3;
y();
console
.log(x
);
}
func(5);
console
.log(x
);
{
function foo() {}
foo
= 1;
}
console
.log(foo
);
{
function foo() {}
foo
= 1;
function foo() {}
}
console
.log(foo
);
{
function foo() {}
foo
= 1;
function foo() {}
foo
= 2;
}
console
.log(foo
);
面试题三:函数柯里化
函数柯里化:必报实现存储值,后期用的机制 已知结果,求函数
let res
= fn(1,2)(3)
console
.log(res
)
function fn (...outerArgs
){
return function anonymous(...innerArgs
){
return outerArgs
.concat(innerArgs
).reduce((a
,b
)=> a
+b
)
}
}
let fn = (...outerArgs
) => {(...innerArgs
)=> outerArgs
.concat(innerArgs
).reduce((a
,b
)=> a
+b
)}
面试题4:函数式编程
函数式编程
const add1 = (x
) => x
+ 1;
const mul3 = (x
) => x
* 3;
const div2 = (x
) => x
/ 2;
function compose(...funcs
){
return function anonymous(...args
){
if (funcs
.length
=== 0) return args
if (funcs
.length
=== 1) return funcs
[0](...args
)
let n
= 0
return funcs
.reduce((a
,b
)=>{
n
++
if (n
=== 1) {
return b(a(...args
))
}
return b(a
)
})
}
}
let result
= compose()(0,1)
console
.log(result
)
let result
= compose(add1
)(0,1)
console
.log(result
)
let result
= compose(add1
,mul3
,div2
,add1
)(0,1)
console
.log(result
)
面试题5:原型链
function Dog(name
) {
this.name
= name
;
}
Dog
.prototype
.bark = function () {
console
.log('wangwang');
}
Dog
.prototype
.sayName = function () {
console
.log('my name is ' + this.name
);
}
function _new() {
}
let sanmao
= _new(Dog
, '三毛');
sanmao
.bark();
sanmao
.sayName();
console
.log(sanmao
instanceof Dog);
function _new(Func
, ...args
) {
let obj
= {}
obj
.__proto__
= Func
.prototype
let result
= Func
.call(obj
, ...args
)
if (result
!== null && (/^(object|function)$/.test(typeof result
))) {
return result
}
return obj
}
面试题6:重写call 方法
~function(){
function change(){
};
Function
.prototype
.change
=change
;
}();
let obj
= {name
:'Alibaba'};
function func(x
,y
){
this.total
=x
+y
;
return this;
}
let res
= func
.change(obj
,100,200);
~function(){
function change(context
, ...args
){
context
= context
== undefined
? window
: context
let type
= typeof context
if(!/^(object|function)$/.test(type
)){
if (/^(symbol|bigint)$/.test(type
)){
context
= Object(context
)
}else{
context
= new context.contructor(context
)
}
}
let key
= Symbol('key')
console
.log(key
, this)
context
[key
] = this
let result
= context
[key
](...args
)
delete context
[key
]
return result
};
Function
.prototype
.change
=change
;
}();
面试题7:重写bind方法
~function(){
function bind(){
};
Function
.prototype
.bind
=bind
;
}();
var obj
= {name
:'zhufeng'};
function func(){
console
.log(this,arguments
);
}
document
.body
.onclick
= func
.bind(obj
,100,200);
~function(){
function bind(context
, ...args
){
let _this
= this
context
= context
== undefined
? window
: context
let type
= typeof context
if(!/^(object|function)$/.test(type
)){
if (/^(symbol|bigint)$/.test(type
)){
context
= Object(context
)
}else{
context
= new context.contructor(context
)
}
}
return function anonymous(...innerArgs
){
_this
.call(context
, ...args
.concat(innerArgs
))
}
};
Function
.prototype
.bind
=bind
;
}();
PS:需配套视频讲解请发邮件到 songjierui5411@dingtalk.com 并备注文章标题