Summary of JS interview questions (1)

1. How to quickly copy an array of data

  • Through slice
var arr = [1, 2, 3], copyArr;
copyArr = arr.slice();
  • Via concat
var arr = [1, 2, 3], copyArr;
copyArr = arr.concat();

2. How to quickly delete the second element of the array

arr.splice(1,1)

3. How to connect arrays to convert strings

arr.join()

4. Understanding of the prototype chain of action

The prototype chain is a (limited) object chain composed of some objects used to inherit and share properties. Each object has its own __poto__ attribute, each function has its own prototype prototype, and the prototype object is also an object, it also has its own prototype object, thus forming a chain of prototype objects, called For the prototype object chain.

5. How to judge whether a prototype is the prototype of this object

Use: Object.prototype.isPrototypeOf() for comparison

var obj1 = {
    
    name: "Lilei"};
var obj2 = Object.create(obj1);
obj1.isPrototypeOf(obj2); // true

6.This point

  • Ordinary function call points to the global object Window
  • Which function is called by the object function, and where does this point
  • The constructor call points to the new instance
  • apply and call call apply and call will change the this of the passed function

7. How to prevent events from bubbling and prevent default events

event.stopPopagation()
event.preventDefault()

8. New Data is converted to a fixed format

var da = new Date();
  var year = da.getFullYear()+'年';
  var month = da.getMonth()+1+'月';
  var date = da.getDate()+'日';
  console.log([year,month,date].join('-'));

9. Which data types are returned by JavaScript typeof

  • String
  • Number
  • Boolean
  • Object
  • Function
  • undefined
  • Symbol

10.Split join difference

  • split() is used to split the string and returns an array
  • join() is used to connect multiple characters or strings, and the return value is a string

Guess you like

Origin blog.csdn.net/qq_44880095/article/details/113357305