js interview questions 2

1. What is the meaning and difference of call () and apply ()?
Meaning:
call (): call a method of an object, replace the current object with another object. For example: B.call (A, args1, args2); is the method that A object calls B object.
apply (): Call a method of an object and replace the current object with another object. For example: B.apply (A, arguments); is the method of applying B object to A object.
Similarities:
The meaning of the method is the same, that is, the method function is the same.
The effect of the first parameter is the same
:
the form of the incoming list is different.
Call can pass in multiple parameters;
apply can only pass in two parameters, so its second parameter is often passed in as an array.
2. Find the maximum value of the array

var nums = [13,54,567,83,-1,-45];
			//apply
			var big = Math.max.apply(this,nums);
			console.log(big);
			//call
			var big2 = Math.max.call(this,13,52,99);
			console.log(big2)

3. The difference between split () and join ()
: join (): used to separate all elements in the array by a specified separator into a string
split (): used to put a string through a specified separator Separated into arrays
4. How to judge whether the current script is running in the browser or node environment?
By judging whether the Global object is window, if not, the current script is not running in the browser.
5. Array method pop () push () unshift () shift ()
pop (): delete an element from the back, the return value is the deleted value;
arr.push (): add an element from the back, the return value is added After the length of the array.
arr.unshift (): Add elements from the front, the return value is the length of the array after the addition.
arr.shift (): Add an element from the front, and the return value is the deleted element.
6.foo = foo || bar, what does this line of code mean? Why is it written like this?
This way of writing is called a short-circuit expression, which is equivalent to

			var foo;
			if(foo){
				foo =foo;
			}else{
				foo =bar;
			}
			//常用于函数参数的空判断

7. This in javascript points to the problem.
Global environment, ordinary function (non-strict mode) execute window
ordinary function (strict mode) execute undefined
function as the object method and the prototype chain points to the upper level object. The
constructor points to the constructed object
DOM The event points to the element that triggered the event
8. Remove the repeated characters in the string

//去除字符串重复内容
			var arr = [...new Set("abbacc")].join("");
			console.log(arr);

9 Ways of event binding

//1.嵌入dom
<button onclick="func">点击</button>
//2.直接绑定
btn.onclick =function(){}
//3.事件监听
btn.addEventListener("click",function(){})

10. The difference between target and currentTarget
event.target: return the element that triggered the event
event.currentTarget: return the element that bound the event
11. Please write it in code (today is week x) where x indicates the day of the week, if the day is Monday, the output should be "Today is Monday"

var days = ['日','一','二','三','四','五','六'];
var date = new Date();
console.log('今天是星期' + days[date.getDay()]);

12. What is the difference between null and undefined?

First of all, Undefined and Null are basic data types. These two basic data types have only one value, namely undefined and null.
The meaning of undefined is undefined, and the meaning of null is an empty object. When a general variable is declared but not yet defined, it will return undefined. Null is
mainly used to assign values ​​to some variables that may return objects as initialization.
undefined is not a reserved word in js, which means that we can use undefined as a variable name, this approach is very dangerous, it
will affect our judgment on the value of undefined. But we can obtain safe undefined values ​​through some methods, such as void 0.
When we use typeof to judge the two types, Null typing will return "object", which is a problem left over by history. When we use the double
equals sign to compare two types of values, it returns true, and when we use three equals signs, it returns false.
13. What is the difference between isNaN and Number.isNaN functions?
After the function isNaN receives the parameter, it will try to convert the parameter to a numeric value. Any value that cannot be converted to a numeric value will return true, so non-numeric values ​​will also
return true, which will affect the judgment of NaN.
The function Number.isNaN will first determine whether the incoming parameter is a number. If it is a number, continue to determine whether it is NaN. This method is more
accurate for NaN .
14. What happens when the Array constructor has only one parameter value?
When the Array constructor takes only one numeric parameter, the parameter will be used as the preset length of the array, rather than just acting as an element in the array. such
What is created is just an empty array, but its length property is set to the specified value.
The constructor Array (...) does not require the new keyword. If not, it will be automatically filled in.
15. Under what circumstances will the implicit coercion of Boolean value conversion?
1. Conditional expression in if (…) statement.
2. for (…;…;…) The conditional expression in the statement (second).
3. Conditional expressions in while (…) and do… while (…) loops.
4.?: Conditional expression in.
5. Logical operators || (logical OR) and && (logical AND) left operand (as a conditional judgment expression).
16. How to convert a string into a number, such as '12 .3b '?
1. Use the Number () method, provided that the included string does not contain illegal characters.

2. 使用 parseInt() 方法,parseInt() 函数可解析一个字符串,并返回一个整数。还可以设置要解析的数字的基数。当基数的值为 0,或没有设置该参数时,parseInt() 会根据 string 来判断数字的基数。

3. 使用 parseFloat() 方法,该函数解析一个字符串参数并返回一个浮点数。

4.使用 + 操作符的隐式转换。

17. What is a closure and why should I use it?
Closures are functions that have access to variables in the scope of another function. The most common way to create closures is to create another function within a function. The created function can
access local variables of the current function.
The first use of closures is to enable us to access variables inside the function outside the function. By using closures, we can call the closure function externally
to access the variables inside the function externally, and we can use this method to create private variables.
Another purpose of the function is to keep the variable object in the function context that has been run to remain in memory. Because the closure function retains the reference of the variable object,
the variable object will not be recycled.
In fact, the essence of closure is a special application of scope chain. As long as you understand the creation process of scope chain, you can understand the realization principle of closure.
18. What is the difference between documen.write and innerHTML?
The content of document.write will replace the entire document content and will rewrite the entire page.
The content of innerHTML only replaces the content of the specified element, and only rewrites part of the content on the page.
19. What is the difference between mouseover and mouseenter?
The mouseenter event is triggered when the mouse moves over the element. Similar to mouseover, the difference between the two is that mouseenter does not bubble.
Because mouseenter does not support event bubbling, mouseover and mouseout events will be triggered when entering or leaving an element's child elements, but mouseenter and mouseleave events will not be triggered.
20. Let's pay attention to const and const?
1. The declared variable is only valid within the code block at the time of declaration
2. There is no declaration promotion
3. There is a temporary dead zone, if it is used before the variable declaration, an error will be reported
4. Repeated declaration is not allowed, repeated declaration will report an error

Published 28 original articles · praised 4 · visits 628

Guess you like

Origin blog.csdn.net/cyang233/article/details/105338419