Front-end interviews in the second half of 2020, I met (1)

front end

Write about the front-end interview questions you have encountered today

  1. There is an array a of unknown length. If its length is 0, add the number 1 to the array, otherwise the first element will be dequeued according to the first-in first-out queue rule.
a.length===0?a.push(1):a.shift()
  1. JS parses the parameters of the url into objects
getUrlKey('http//aaa/txt.php?a=1&b=2&c=3')
function getUrlKey(url){
    
    
	let params={
    
    }
	let urls=url.split('?');
	let arr=urls[1].split('&');
	for(let i=0;i<arr.length;i++){
    
    
		let a=arr[i].split('=');
		params[a[0]]=a[1]
	}
	return params;
}
  1. Output the following console.log
 var a = 10;
    (function () {
    
    
        console.log(a);
         a = 20;
        console.log(window.a);
        var a = 30;
        console.log(a)
    })()
   //结果:undefined 10 30
  1. Output the following console.log
    let a = 1;
    (function a(){
    
    
        a = 2;
        console.log(a);
    })();
    console.log(a);

//结果:
    ƒ a(){
    
    
    a = 2;
    console.log(a);
} 

1
//(function (){})() 是自执行函数
  1. Write the following result:
  function Foo() {
    
    
        this.a = function () {
    
    
            console.log(1)
        };
        Foo.a = function () {
    
    
            console.log(2)
        }
    }

    Foo.prototype.a = function () {
    
    
        console.log(3)
    };
    Foo.a = function () {
    
    
        console.log(4)
    };

    Foo.a();
    let obj = new Foo();
    obj.a();
    Foo.a();
    
    // 结果:4 1 2
  1. Write the following result:
	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();//1
	getName();//1
	new Foo.getName();//2
	new Foo().getName();//3
	new new Foo().getName();//3
  1. var s="are you ok", write js to become are| you| okay|
function split(){
    
    
	var str="are   you okay",
	res=""

	var arr=str.trim().split(/\s+/)
	for(i=0;i<arr.length;i++){
    
    
		 res+=arr[i]+"| "
	}
	return res;
}

  1. Write a sum method that can work normally when called in the following way [curry]
console.log(sum(2,3))// 5
console.log(sum(2)(3))//5
     function add() {
    
    
        // 第一次执行时,定义一个数组专门用来存储所有的参数
        var _args = [].slice.call(arguments);
       

        // 在内部声明一个函数,利用闭包的特性保存_args并收集所有的参数值
        var _adder = function() {
    
    
            _args.push(...arguments);

            return _adder;

        };

        // 利用toString隐式转换的特性,当最后执行时隐式转换,并计算最终的值返回
        _adder.toString = function () {
    
    
            return _args.reduce(function (a, b) {
    
    
                return a + b;
            });
        };
        return _adder;
    }

    add(2,3);
    add(2)(3);

  1. What does the following expression get?
	4+3+2+1”

	答案:“914+3+2-1”

	答案:8
  1. Are the arguments in the function an array? If not, how to convert it into a real array?
    Not an array
function func(){
    
    
console.log(arguments);

return [ ].slice.call(arguments);
}
console.log(func(1,2,3))
  1. What is the output of the following code execution, and clarify the reason
var x=3;

var foo={
    
    
	x:2,
	baz:{
    
    
		x:1,
		bar:function(){
    
    
			return this.x;
		}
	}
}

var go=foo.baz.bar;
console.log(go());	//第一个
console.log(foo.baz.bar()); //第二个

this的问题,
首先我们要看第一个,这时this指向的是window
其次看第二个,调用对象是foo.baz
所以这时this指向的是x:1
  1. What is the output of the following code after execution, and explain the analysis process.
let x=0;

async function test(){
    
    

	x+=await 2;
	console.log(x);
}

test();
x+=1;
console.log(x);

//答案:1 , 2

这里相关的就是同步还是异步

我们程序执行的时候,一般是所有同步的都结束之后,

再执行微任务,然后宏任务

所以asyn 这函数是需要后面执行的

Guess you like

Origin blog.csdn.net/weixin_43814775/article/details/111824919