This is the fifth day of my participation in Gwen Challenge

preface

I collected some front-end related basic questions on the Internet, and kept doing one question every day for 200 days, and compiled the answers myself.

Now 200 questions are summarized here, hoping to help some students who just touch the front end, big guy please go around

Questions 1 to 10

1. What is the result of executing the following code

function getInfo(member, year) {
	member.name = "Alice";
	year = "2020"
}
var person = { name: "Tom" }
var brithYear = "2010";
getInfo(person, brithYear);
console.log(person, brithYear);
Copy the code

Answer: {} name: Alice, 2010

2. What is the result of executing the following code

var p = new Promise(resolve= > {
    console.log(4);
    resolve(5);
});
function func1() {
    console.log(1)}function func2() {
    setTimeout(() = > {
        console.log(2)}); func1();console.log(3);
    p.then(resolved= > {
        console.log(resolved)
    }).then(() = > {
        console.log(6)}); } func2();Copy the code

4, The event loop mechanism, the main program execution, and then execute micro queue, and then execute macro queue; Promise then goes into microqueue first, setTimeout goes into macro queue

3. What is the result of the following code execution

function Car(){
    this.name = "BMW"
    return {
        name:"maserati"}}var car = new Car();
console.log(car.name);
Copy the code

When the new constructor generates an object, it returns an object, and this is the object

4. Simulate the placeholder effect

<input type="text" name="user" value="Please enter user name" style="color:#999"
onfocus="If (this.value==' please enter user name '){this.value ="; this.style.color='#424242'}"
onblur="If (this.value =="){this.value = 'Please enter user name '; this.style.color='#999'}" >
Copy the code

5. What is the result of executing the following code

var a = 1;
var b = 2[a,b] = [b,a]
console.log(a,b)
Copy the code

A = 1; B = 1

6. What is the last printed ARR in the following code

var arr = [1.2.3.4.5];
arr.length = 1;
Copy the code

[1] The length of the array is affected by the length of the array

7. What is the result of the following code execution

var arr = [1+1.1*2.1/2];
console.log(arr);
Copy the code

[2,2,0.5] when an array is saved, each bit of the expression is evaluated first

8. The result of the following code is

var one = (false| | {} | |null);
var two = (null || false || "");
var three = ([] || 0 || true);
console.log(one,two,three);
Copy the code

Answer: {}, “”, [] analytic: subject examines | | the use of the operator. Actually remember one rule: | | operators return is to be able to confirm the value of the final result!

9. What is the result of the following code execution?

var h5course = false;
var result = h5course / 0;
if (result) {
    console.log(result * 2 + '2' + 4)}else {
    console.log(! result *2 + '2' + '4')}Copy the code

Answer: “224” analytic: 0/0, null / 0, undefined / 0, false / 0 is NaN, other positive/zero to infinity, the other negative number divided by zero – infinity. ! NAN is true, and true*2 is 2

10. What is the result of the following code execution?

var a = 0, b=0;
function A(a){
    A = function(b){
        alert(a + b++)
    }
    alert(a++)
}
A(1)
A(2)
Copy the code

Deep precompilation and closures will yield results

11-20

11. What is the result of executing the following code?

var str = new Array(5).toString()
console.log(str);
Copy the code

New Array(n) returns an Array whose length is n and each bit is empty. Var arr = [5] = var arr = [5]

12. What is the result of the following code?

console.log(123..toString());
Copy the code

A new Number(123.) is used to package 123 into a wrapper object. Call is Number. The prototype. ToString, so new Number (123). The toString = = = Number. The prototype. ToString

13. What is the result of executing the following code?

var x = 1;
var y = 2;
function show() {
    var x = 3;
    return {
        x: x,
        fun: function (a, b) { x = a + b; }}}var obj = show();
obj.fun(x, y);
console.log(obj.x)
console.log(x)
Copy the code

3,1. Closures and precompilations

  1. What is the result of the following code execution?
var a = (true + false) > 2 + true;
console.log(a);
Copy the code

[答案] : it involves the calculation order and type conversion of knowledge operators. There is an implicit conversion of true + false to 1+0. The arithmetic operation takes precedence over the comparison operator, so 2+1 is used first and the comparison is: 1>3 to get false

15. Talk about the precedence of the operator

[parentheses ()] > [logic not!] > > count * / % 】 【 count + – > 】 【 relationship more > < = =] > [logic and &&, logic, or | |] > [ternary? :] > [assignment =] is roughly such, specific can view this document, there is a table: Developer.mozilla.org/zh-CN/docs/…

16. What is the result of the following code execution?

var num = 3;
console.log(num.toString(2));
console.log(num.toFixed(2));
Copy the code

The toString method is used to convert a number into a base of 10. The toString method is used to convert a number into a base of 10. The toString method is used to convert a number into a base of 10. The toFixed method changes the target number to a decimal number that retains the specified number of digits. Note the rounding! Let’s leave two decimal places here, so it becomes 3.00

17. What is the result of the following code?

function a(x){
    return x*2
}
var a;
console.log(a);
Copy the code

Function a(x){return x*2}

18. What is the result of the following code?

function fun(n, o) {
    console.log(o);
    return {
        fun: function (m) {
            returnfun(m, n); }}}var a = fun(0);
a.fun(1);
a.fun(2);
a.fun(3);
var b = fun(0).fun(1).fun(2).fun(3);
var c = fun(0).fun(2);
c.fun(2);
c.fun(3);
Copy the code

The answer:

undefined 0 0 0

undefined 0 1 2

undefined 0 2 2

What is the result of executing the following code?

var a = []+[]+"alice".split("");
console.log(a);
Copy the code

The answer: “a, l, I, c, e” analytic: [] + [] to an empty string “, an empty string addend group, equal to the form of an array into a string, call the toString is equivalent to an array

20. There are two variables A and B, whose values are of type number and non-nan, which complete the exchange of A and B without the help of other variables

Var a = 101; var a = 101; var b = 102; a = a + b; b = a – b; a = a – b; console.log(a,b)

21-30 items

What is the result of executing the following code?

function Point(x, y) {
    this.x = x;
    this.y = y;
    this.moveTo = function (x, y) {
        this.x = x;
        this.y = y;
        console.log(this.x, this.y)
    }
}
var p1 = new Point(0.0);
var p2 = { x: 0.y: 0 };
p1.moveTo(1.1);
p1.moveTo.apply(p2, [10.10])
Copy the code

This refers to two things: (1) this refers to who calls the function, and (2) apply call bind changes this when the function executes

22. What is the result of executing the following code?

var globalVar = 0;
(function (outerArg) {
    var outerVar = 456;
    (function (innerArg) {
        var innerVar = 10;
        console.log(globalVar, outerArg, outerVar, innerArg, innerVar); } (789))

}(123))
Copy the code

There is no difference between executing a function immediately and executing a normal function

23. What is the result of the following code execution?

console.log(1 + undefined);//NaN
console.log(1 + null);/ / 1
console.log(null + undefined);//NaN 
console.log(true + false);/ / 1
console.log(1 + '2');/ / 12
console.log(2 + []);/ / 2
console.log(2 + { a: 1 });//2[object Object]
console.log([] + {});//[object Object]
console.log({} + []);//[object Object]
console.log(3 + {});//3[object Object]
console.log({} + 3);//[object Object]3
Copy the code

Answer: As above

[object object] [object object] [object object] [[object object]

24. What is the result of the following code? why

var a = {},b={key:"b"},c={key:'c'};
a[b] = 123;
a[c] = 456;
console.log(a[b]);
Copy the code

If the attribute is passed in as an object, js will implicitly change the variable to the corresponding object. A [b] and a[c] are both equivalent to a[“[object object]”], so they both operate on the same attribute of a object, so the result is 456

25. What is the result of executing the following code?

for (var i = 0; i < 5; i++) {
    setTimeout(function () {
        console.log(i);
    }, i * 1000);
}
Copy the code

For (let I = 0; let I = 0; let I = 0; i<5; i++){ setTimeout(function(){ cosnole.log(i); },i1000)} for (var I = 0; i < 5; i++) { (function(i){ setTimeout(() => { console.log(i)//0 1 2 3 4 }, i100); }(i)) }

26. What is the result of the following code execution?

var user = "Alice"
function changeUser(){
    user  = "Tom";
    return;
    function user(){
        console.log('user function')
    }
}
changeUser();
console.log(user);
Copy the code

Answer: Alice

ChangeUser = “Tom” (); changeUser = “Tom” ();

27. What is the result of the following code execution?

(function(){
    var user = author = "Alice"; } ());console.log(author);
console.log(user);
Copy the code

Answer: Alice, Uncaught ReferenceError: User is not defined

There is no user in the global directory, so an error will be reported. Author implies that the global variable has a value.

What is the result of executing the following code?

console.log(0 || 1);
console.log(1 || 2);
console.log(0 && 1);
console.log(1 && 2);
Copy the code

The answer is: 1, 1, 0, 2

Analytic: | | or operation, the front is true, to return to the previous, to false front, back to back

The && and operator is false, returns the preceding, true, returns the following

29. What is the result of the following code?

console.log(1 + '2' + '3');
console.log(1 + +'2' + '3');
console.log(1 + -'1' + '2');
console.log(+'1' + '2' + '3');
console.log('A' - 'B' + "2");
console.log("A" - "B" + 2);
Copy the code

Answer: ‘123’ ’33’ ’02’ ‘123’ ‘NaN2’ NaN parse: (1) Any data type + string is a string; (2) Unitary positive and negative can be changed into numerical type; (3) Non-numeric or non-numeric string The value is NaN

30. What is the result of the following code?

(function(){
    try{
        throw new Error
    }catch(x){
        console.log(x)//Error
        var x = 1;
        var y = 2;
        console.log(x)/ / 1
    }
    console.log(x);//undefined
    console.log(y);/ / 2}) ();Copy the code

Answer: 1 undefined 2

Parsing: the function parameters are initially executed anonymously and immediately in scope. The variables x and y are raised to undefined due to precompilation

When the catch function is executed, it has its own parameter x, where the value is an Error message: Error, followed by the value 1; So catch prints Error first and then 1

Y is assigned 2;

So the value printed outside: x is undefined, y is the changed value 2

31 to 40 items

31. What is the result of executing the following code?

var a = 10;
function fn() {
    console.log(this.a)
}
var obj = {
    a: 5.method: function (fn) {
        fn();// execute this by itself, all this points to window, and print 10
        arguments[0] (); } } obj.method(fn,1)
Copy the code

Answer: 10 undefined

This refers to the window.

This function takes another function as an argument and is called as arguments.0(). Arguments.0 stands for this function, so this refers to arguments.

Such as:

var length = 10;
function fn() {
    console.log(this.length)
}
var obj = {
    length: 5.method: function (fn) {
        fn();// execute this by itself, all this points to window, and print 10
        arguments[0] ();Arguments.0 () = fn (fn); // (fn) = fn (fn); Print out 2
    }
}
obj.method(fn, 1)
Copy the code

The answer is: 10, 2

32. The result of executing the following code

var fn = function a(){
    a = 1;
    console.log(typeof a)
}
fn();//function
console.log(typeof a)//undefined
Copy the code

A. function b. function C. function D. function

There are two things to note about named function expressions:

(1) The function name of a named function expression (a) also represents a function in the function body

(2) The name of the function is automatically ignored outside the function body, that is, it is not accessible outside the function body (a)

(3) Inside the function body, the function name (a) cannot be modified

What is the difference between this and $(this) in jQuery?

Answer: This is one of the JS keywords, representing the current DOM element; $(this) returns a jQuery object that can call methods wrapped in jQuery

34. What is the result of the following code?

function fn1() {
    return {
        str: 'hello'}}function fn2() {
    return
    {
        str: 'hello'}}console.log(fn1());
console.log(fn2());
Copy the code

{STR: “hello”} undefined: undefined: js; ‘

35. How do I get the number of function arguments and parameters?

The answer:

function fn(a,b){
    //1. Get the number of arguments:
    console.log(arguments.length);/ / 5
    //2. Get the number of parameters:
    console.log(arguments.callee.length)/ / 2
    arguments.callee===fn, so you can also use fn.length to get the number of parametersconsole.log(fn.length)/ / 2
}
fn(1.2.3.4.5);
Copy the code

36. What is the result of the following code execution?

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)}// Analyze what is present in GO: {
// getName: function getName() {alert(4)},
// Foo: function Foo(){getName = function () {alert(1)} return this; }
// }
Foo. GetName = function () {alert(2)}
GetName = function () {alert(3)}
// What is the result below?
Foo.getName();/ / 2
getName()/ / 4
Foo().getName()//Foo() returns this as window, and getName in GO becomes alert(1), so: 1
getName()/ / 1
new Foo.getName()// Foo. GetName is executed as a constructor, so: 2 pops up
new Foo().getName();// Call the method on the prototype so print: 3
new new Foo().getName()// The priority of the js operator is equivalent to new((new Foo()).getName)(); Answer output: 3
Copy the code

Answer: As above

37. What is the result of the following code?

var user = 'alice';
function firstFn(){
    console.log(user);
}
function secondFn(){
    var user = 'tom';
    firstFn();
}
firstFn();
Copy the code

The answer: ‘Alice’

In this example, the scope of secondFn is not added to firstFn.

It is important to note that adding to a scope chain depends primarily on where the function is defined, not where it is used.

38. How do I prevent the default jump event of tag A?

The answer:

let tagA = document.getElementsByTagName('a') [0]
taga.onclick = function (e) {
    //w3c
    // e.preventDefault();
    / / IE unique
    // e.returnValue = false;
    // Both can be used
    return false;
}
Copy the code

39. How do I make an element undisplayed

The answer:

let odiv = document.getElementsByClassName("div") [0]
1 / / method
odiv.style.display = 'none'
2 / / method
odiv.style.visibility = "hidden"
3 / / method
odiv.styly.opacity = 0
Copy the code

40. What is the result of the following code?

let arr = [1.2.3.4]
arr[10] = 10
arr[20] = 20
console.log(arr[15])
Copy the code

Answer: undefined

Overflow reads and writes are available. The value of overflow reads is undefined, and the value of overflow writes is undefined

41 to 50

41. What is the result of the following code?

console.log(typeof undefined= =typeof NULL)
Copy the code

Answer: true,

Js is case-sensitive,NULL is not NULL, typeof NULL returns object

42. The result of executing the following code is

function fn(xx){
    this.x = xx
    return this
}
var x = fn(5)
var y = fn(10)
console.log(x.x)
console.log(y.x)
Copy the code

Answer: undefined 10

At fn(5), window.x === window; When fn(10) executes, window.x = 10, window.y === window;

X = new Number(10). X = undefined, y.x = window. X = 10

43. What is the result of the following code?

var x = 5;
function fn(x){
    x = 8
    console.log(x)
}
fn()
console.log(x)
Copy the code

Answer: 8, 5

Fn prints x as local variable x, so print 8 first, and print x as global window, so print 5

44. The result of executing the following code is

(function(x){
    return (function(y){
        console.log(x)
    })(2); }) (1)
Copy the code

Answer: 1.

When the first function executes immediately, there is an x value of 1 on the AO, and then return the second function executes immediately, there is still access to x, so it prints 1

45. What is the output of the following code?

var result = (function f(n) {
    return (n > 1)? n * f(n -1) : n
})(10)
console.log(result)
Copy the code

Answer: 10 classes, namely 3628800

46. What is the result of executing the following code and why?

function fn(){
    var a = b = {}
    a.name = 'alice'
    b.age = 10
    console.log(b.name,a.age)
}
console.log(typeof a)
console.log(typeof b)
Copy the code

Alice 10 undefined object

The assignment of a reference value is an address, which refers to an object. Indicates that the global variable is owned by the window

47. What is the result of the following code?

console.log(+true)
console.log(!'str')
Copy the code

Answer: 1, false

Unary + / – default implicit call Number(); ! Boolean() is called when the default is reversed, that is! ‘STR’ equals Boolean(‘ STR ‘)

48. What is the result of the following code?

function Person(firstName,lastName){
    this.firstName = firstName
    this.lastName = lastName
}
let p1 = new Person('Lewis'.'Carroll')
let p2 =  Person('Lewis'.'Carroll')
console.log(p1)
console.log(p2)
Copy the code

Person{fristName: ‘Lewis’, lastName: ‘Carroll’}, undefined

What is the result of executing the following code?

function show(){
    "use strict";
    // name = "alice";
    age = 12;
    // console.log(' ${name} this year ${age} age ')
}
show()
Copy the code

In strict mode, variables must be declared before assigning, otherwise ReferenceError

If a variable is assigned to a string without being declared in strict mode, no error is reported. Further verification is pending

50. What is the result of the following code?

(function(){
    console.log(1)
    setTimeout(() = > {
        console.log(2)},1000);
    setTimeout(() = > {
        console.log(3)},0);
    console.log(4)
})()
Copy the code

Answer: 1, 4, 3, 2

A. queue b. queue C. queue D. queue

51 to 60

51. In the following code, what does event. Target print when a button is clicked?

<div onclick="console.log('first div',event.target)">
    <div onclick="console.log('second div')">
        <button onclick="console.log('button')">click me!</button>
    </div>
</div>
Copy the code

Answer: event.target prints –> Click me!

When tutton is clicked, the same event is triggered because the event bubbles up to the parent element. Event. Target records the target source of the event

52. What is the result of the following code execution?

function checkAge(data) {
    if (data === { age: 18{})console.log('I'm 18 years old.')}else if (data == { age: 18{})console.log('I came of age this year')}else {
        console.log('emmm... ')
    }
}
checkAge({ age: 18 })
Copy the code

Answer: emmm…

The reference value compares the address

53. What is the result of executing the following code?

let count = 0
console.log(count++)
console.log(++count)
console.log(count)
Copy the code

Answer: 0, 2, 2

If ++ is followed by +1, execute the statement then +1; If ++ precedes, the number is +1 before the statement is executed

54. How do I know if a variable is an array

let arr = []
1 / / method
arr.contructor === Array
2 / / method
arr instanceof Array
3 / / method
Array.isArray(arr)
Copy the code

55. What is the use of push pop unshift in the array shift?

Push: adds one or more bits to the end of the array and returns the changed array length

Pop: Removes one bit from the end of the array and returns the removed element

Unshift: Adds one or more bits to the front of the array. Returns the changed array length

Shift: Removes the top bit of the array, returning the removed element

Note: All of the above methods change the original array

56. What is the result of the following code?

(function(){
    var x = y = 12 
})()
alert(y)
Copy the code

Answer: 12

Parsing: Indicates that a global variable is assigned an undeclared value that belongs to the window

57. What is the result of the following code?

var x = 'global'
function fn1(){
    console.log(x)
}
function fn2(){
    var x = 'fn2'
    fn1()
}
fn2();
Copy the code

Answer: the global

The scope is static and depends not on where the function is executed, but on where the function is defined.

58. What is the result of the following code?

var a = 1
var b = a
b++
console.log(a, b)
Copy the code

A primitive assignment is a copy of a value

59. What is the result of the following code?

var a = [1.2.3.4]
var b = a;
a.push(5)
console.log(a,b)
Copy the code

[1,2,3,4,5] [1,2,3,4,5] an assignment of a reference value is a copy of the address, pointing to the same space

60. What is the result of the following code?

var x = 5;
function F(){
    x = 12;
    console.log(x);
    console.log(this.x);
    var x;
    console.log(x)
}
new F()
Copy the code

Answer: 12 undefined 12

A. new B. new C. new D. new Test point 2 precompile

Pp. 70-61

61. What is the result of the following code?

var arr = [1.2.3.4]
delete arr[1]
console.log(arr.length)
delete arr[3]
console.log(arr.length)
Copy the code

The value of the arR array is empty, but the length of the array remains the same

62. What is the result of executing the following code?

var res = (12).toFixed(2)
console.log(res)
Copy the code

A. number B. package C. number D. package 12. ToFixed (2) = 12. XXX!

63. What is the result of the following code?

console.log(12 + '5')
console.log(12 - '5')
Copy the code

Answer: 125, 7

If one of the numbers in the + sign operator is a string, the + sign is used as a string concatenation. The – operator converts the Number() from both sides to a Number and subtracts it

64. What is the result of executing the following code?

var a = new Array(3);
var b = [undefined.undefined.undefined];
var c = [null.null.null];
var d = [false.false.false];
var e = [0.0.0];
console.log(a.join(The '-'));
console.log(b.join(The '-'));
console.log(c.join(The '-'));
console.log(d.join(The '-'));
console.log(e.join(The '-'));
Copy the code

The answer:

false-false-false

0-0-0

The join method assembles the array members into the specified character concatenation. In particular, if the array member of a JOIN is null or undefined, it will be treated as an empty string

65. How do I get a random integer greater than or equal to 0 and less than or equal to 9?

console.log(Math.floor(Math.random()*10))
Copy the code

66. What is the result of the following code?

function sum(a, b) {
    return a + b;
}
console.log(sum(1.'2'));
Copy the code

The answer: “12”

Parsing: String concatenation

67. Which objects don’t have archetypes?

Answer :(1) the base top-level object; (2) The Object created by object.create (null)

68. What is the result of the following code?

var obj = {
  a: 1,
  b: 2,
  a: 3
}
console.log(obj)
Copy the code

Answer: {3, a: b: 2}

Resolution: Variables in non-strict schema objects can be repeated, but the following will overwrite the preceding

69. What is the result of executing the following HTML code?

<div onclick="console.log('div')">
    <button onclick="console.log('btn')">click me!!</button>
</div>
Copy the code

Answer: BTN div

Structurally non-visually nested elements have event bubbling, which bubbles from child elements to parent elements

70. What is the result of the following code?

console.log(typeof typeof 12)
Copy the code

The answer: ‘string’

Any type returns a string after the first typeof

Pp. 80-71

71. What is the result of the following code?

var arr = [1.2.3];
arr[10] = 4;
console.log(arr);
Copy the code

Answer: [1, 2, 3, empty * 7, 4]

An array can be overwritten, with empty in the middle and undefined when read

72. What is the result of the following code?

String.prototype.showInfo = ()=>{ return 'This is showInfo! ' } let str = 'aaa'; let res = str.showInfo(); console.log(res);Copy the code

Answer: ‘This is showInfo! ‘

Parsing: Wrap classes and stereotypes

73. What is the result of the following code?

console.log(!!undefined)
console.log(!!null)
console.log(!!' ')    
console.log(!!0)
console.log(!!1)
Copy the code

False false false true The operator is used to convert a value to a Boolean value

What is the result of executing the following code?

console.log(1>2>3);
console.log(3>2>1);
console.log(1<2<3);
console.log(3<2<1);
Copy the code

False false true: compare the result from left to right, compare the result from left to right, and convert any value to a number first

75. What is the result of the following code?

let  arr = [1.2.3];
arr.unshift('a');
arr.push('b');
let newArrr = [4. arr,5];
console.log(newArrr);
Copy the code

[4,’a’,1,2,3,’b’,5

76. What is the result of the following code?

console.log('12'/0);
console.log(0/0);
console.log(true= =1);
console.log(NaN= = =NaN);
Copy the code

答案 : infinity NaN true false

77. What is the result of executing the following code?

let a = 1;
a = (++a, a++);
console.log(a);
a = (a++, ++a);
console.log(a);
Copy the code

Answer: 2, 4

The ++ operator and the comma operator.

++ is used to execute the statement before ++ is used to execute the statement after ++

The comma operator returns the result after the comma, and of course the preceding statement is executed

78. There are several ways to define functions in js:

(1) Function declaration

function fn1(){}

(2) Function expression

let fn2 = function(){}

(3) through the Function constructor

let fn3 = new Function(‘a’,’b’,’return a+ b’)

Function The last parameter represents the body of the Function, and the remaining parameters are parameters

79. What is the result of the following code?

function Person(){}
console.log(Person.prototype === Function);
console.log(Person.prototype === Function.prototype);
console.log(Person.__proto__ === Function.prototype);
console.log(new Person().__proto__ === Person.prototype);
Copy the code

Answer: False false true true

Analysis: About prototypes. A stereotype is a property of a function object that defines the common ancestor of the object that the constructor constructs

80. What is the result of the following code?

function f(){
    f = 12;
    console.log(f);
}
console.log(f);
f();
console.log(f);
Copy the code

F (){} 12 12

Pp. 90-81

81. What is the result of the following code?

var fn = (function(temp){
    arguments[0] = 'Hello';
    return function(){
        console.log(temp,arguments[0])}}) ('Mike')
fn('Alice');
Copy the code

A. function b. function C. function D. argument

What is the result of executing the following code?

if(function fn(){{})console.log(typeof fn)
}
console.log(typeof fn)
Copy the code

Answer: ‘undefined’ ‘undefined’

Parsing: Inside the parentheses of an if statement, JS automatically changes the contents to the corresponding Boolean value via Boolean(), so the function declaration cannot use the function elsewhere

83. What is the result of executing the following code?

console.log(1..toString())
console.log(NaN.toString())
console.log(typeof temp) 
console.log(1+ +)Copy the code

Answer: the first three are: ‘1’ ‘NaN’ ‘undefined’;

Uncaught SyntaxError: Invalid left-hand side expression in postfix operation

Parsing: because js execution will first scan to see if there are syntax errors, and then execute JS statements line by line. If the syntax is wrong, no other statements are executed. So there’s a SyntaxError

84. Talk about call and apply functions

(1) Call and apply are functions defined on function. prototype; each Function has call and apply methods

(2) When a function is called and the call or apply method is called, call and apply change the this pointer in the function and execute the function

(3) The only difference between call and apply is in the form of parameter passing.

The first argument to call is this, and the second argument starts with the function argument,

The first argument to apply also refers to this, and the second argument is an array, each of which corresponds to a parameter

(4) Another function similar to call and apply, bind(). This function simply binds the function’s this pointer and does not execute it immediately.

85. What is the result of executing the following code?

var obj = {
    name: 'obj'
}
var bar = {
    name:'bar'.show:function(){
        var name = 'foo'
        console.log(this.name)
    }
}
bar.show.call(obj);
Copy the code

Answer: ‘obj’

86. What is the result of the following code?

var val = 1
var fn = function(){
    var val = 2;
    return function(){
        console.log(this.val)
        console.log(val)
    }
}
fn()()
Copy the code

Function (){} function(){} function(){} function(){} function(){} function(){} function(){} function(){} function(){} function

87. What is the result of the following code execution?

var a = function () {
    this.b = 2;
}
a.prototype.b = 20;
b = new a();
console.log(b.b);
var b = 1000;
a();
console.log(this.b)
Copy the code

Answer: 2,2

When b is new, there is a b in it, so there is no need to look for it in the prototype

When a() is executed, window.b=2 is executed because this refers to window. So this. B is window.b, which prints 2

88. What is the result of the following code?

var a = { n: 1 }
var b = a
a.m = a = { m: 2 }
console.log(a)
console.log(b)
Copy the code

{m:2} {m: 1,m:{m:2}} {m: 1,m:{m:2}} {m: 1,m:{m:2}} {m: 1,m:{m:2}} Perform the assignment from right to left. So start a and b to space {n: 1} into {n: 1, m: {2} m:}, and is to assign a value to a {2} m:, so eventually fired a is {2} m: as a result, b for {n: 1, m: {2} m:}

89. What is the result of executing the following code?

function Foo(){}
var f1 = new Foo()
console.log(f1.constructor)
Foo.prototype = {}
var f2 = new Foo()
console.log(f2.constructor)
Copy the code

Foo(){}, Object(){} : note that constructor is something defined on a prototype, so it finds its own prototype first and then sees what its constructor points to. Thus, the constructor of new instance F1 without changing the stereotype points to Foo, and the constructor of new Object F2 after changing the stereotype is Object(){}.

What is the result of executing the following code?

function Person(){
    this.name = 'alice'
    return true;
}
var p = new Person();
console.log(p)
Copy the code

Person {name: “Alice “}

If the constructor returns a reference value, this is overwritten. If the constructor returns a reference value, this is overwritten

Pp. 100-91

91. What is the result of executing the following code?

var val = 1;
val.length = 2;
console.log(val.length)
Copy the code

1, Use undefined to call an attribute of the original type. In fact, js internally changes the original value into a wrapper object, and deletes the object after execution. So val. Length is undefined

92. What is the result of the following code

var fn = function a(){
    console.log(a)
    a = 1;
    console.log(a)
}
fn();
console.log(a)
Copy the code

答案 : function a(){… } function a(){… } Uncaught ReferenceError: a named function expression is not defined. The function name can be accessed inside the function, but cannot be modified.

93. What is the result of executing the following code?

function a() {
    console.log(1)}function b() {
    console.log(2)}function c() {
    console.log(3)}function d() {
    console.log(4)}false && a();//&& is false, so a() is not called
true || b();/ / | | front to true won't look back, so there is no b ()
false || c();/ / | | front to false will look back, so here do c (), print out 3
true && d();//&& is true and will be looked at later, so d() is called to print 4
Copy the code

Answer: 3 and 4

Analytic: main && and | | operators, the characteristics of principle is based on the true and false values to determine whether to perform at the back of the expression

What is the result of executing the following code?

function fn(a){
    let a = 10;
    console.log(a)
}
fn(1)
Copy the code

5, Uncaught SyntaxError: Identifier ‘a’ has already been declared

95. What is the result of the following code?

function fn1(){
    var a = 1;
}
function fn2(){
    return;
}
console.log(fn1(),fn2())
Copy the code

Undefined: returns undefined by default; Return is the same thing as not saying return, which is undefined

96. Handwritten Grail inheritance

var inherit = (function () {
    function F() {};return function (Target, Origin) {
        F.prototype = Origin.prototype;
        Target.prototype = new F();
        Target.prototype.constructor = Target;
        Target.prototype._super = Origin.prototype;
    }
}());
Copy the code

Encapsulate a function mul that calculates the hierarchy of n

function mul(n){
    if(n==0||n==1) {return 1;
    }
    return n*arguments.callee(n-1);
}
Copy the code

98. Encapsulate a function bytesLengh to find the length of any string in bytes

function bytesLengh(str) {
    var count = str.length;
    for (var i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 255){ count++; }}returncount; } charCodeAt(n) : Returns the Unicode encoding of the NTH character of the string, greater than255The byte length is2, less than1
Copy the code

99. Encapsulate a method, insertAfter, that functions like insertBefore

Element.prototype.insertAfter = function (targetNode, afterNode) {
    var beforeNode = afterNode.nextElementSibling;
    if (beforeNode) {
        this.appendChild(targetNode);
    }
    this.insertBefore(targetNode, beforeNode);
}
Copy the code

100. Manually encapsulate asyncLoadScript, a method that loads JS asynchronously

function asyncLoadScript(url, callback) {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    if (script.readyState) {//readyState is the status code in IE
        script.onreadystatechange = function () {
            // Bind the event that listens to the status code, IE status code becomes complete or loaded, indicating that the element is finished loading
            if (script.readyState == "complete" || script.readyState == "loaded") {
                callback();// The callback function is called when the script is finished loading}}}else {
        // Non-IE uses the onload event to indicate when script has finished loading
        script.onload = function () {
            callback();// The callback function is called when the script is finished loading
        }
    }
    script.src = url;Onreadystatechange = onReadyStatechange = onReadyStatechange
    document.head.appendChild(script);// Load it into the page
}
//=============test code=========================
asyncLoadScript('./js/tools.js'.function () {
    //code
    console.log('Load as finished:' + url + 'file')});Copy the code

Pp. 110-101

  1. How to empty and truncate array ARR
Var arr = [1, 2, 3, 4]Copy the code

Answer: empty: arr. Length = 0; Truncate: arr. Length = n; Where n indicates the position to be truncated

  1. What is the result of executing the following code?
Var arr = (1, 0, ' ', null, and undefined, false, true, NaN] var res = arr. Filter (Boolean) console. The log (res)Copy the code

[1,true] [1,true] [1,true] [1,true] [1,true] [1,true]

  1. Encapsulates a method, hasClass, that checks if an element has a specified class name
function hasClass(el,className){
    return el.classList.contains(className)
}
Copy the code
  1. How to calculate the block-level element width under the standard box model?

Answer: Element width = Content width + left/right padding + left/right border

  1. What is the difference between the substring and slice functions in string objects?

(1) Both substring and slice are used for string interception. (2) Both start and end parameters are accepted to indicate the start and end index bits, respectively. (1) If end<start substring swaps start and end; If start or end has a negative number, substring will count numbers less than 0 as 0. Slice will allow negative numbers, indicating the beginning or end of the penultimate digit

  1. Font-family: arial, sans-serif; mso-font-kerning: 0pt; mso-font-kerning: 0pt;
Structure <div class="p2"> <div class="s5">1</div> <div class="s6">1</div> </div> style. line-height: 2; } .s5 {font-size: 2em; } .s6 {font-size: 2em; line-height: 2em; }Copy the code

Font: 16px; S5: Font-size: 32px; Font-family: Arial; mso-font-kerning: 0pt; mso-font-kerning: 0pt; S5 = 0; s5 = 0; s5 = 0; s6 = 0; s6 = 0

  1. Talk about CSS weights

! Important infinity interline style 1000 ID 100 Class pseudo-class attribute 10 tag pseudo-element 1 Wildcard 0

  1. How do I get the height and width of the browser window

Window. innerHeight window.innerWidth

109. What is the result of the following code?

var i = 0, j = 0;
for (; i < 10, j < 5; i++, j++) {
    k = i + j
}
console.log(k)
Copy the code

Note the order in which the for loop is executed

  1. Find the maximum value in array ARR and write three methods
Var Max = [1,3,4,-1,0,10,15,7] var Max = [function (a, b) {return b - a})[0] var Max = [1,3,4,-1,0,10,15,7] var Max = [1,3,4,-1,0,10,15,7] arr); Var Max = -infinity for (var I = 0; i < arr.length; i++) { if (max < arr[i]) { max = arr[i] } }Copy the code

Pp. 120-111

  1. Find the result of executing the following code
var a = 1
var b = a++
console.log(a, b)
var x = 1
var y = ++x
console.log(x, y)
Copy the code

Note the difference between the ++ operator and the ++ operator

112. What is the result of executing the following code?

function fn(){
    alert(1)
}
alert(fn())
Copy the code

If fn() returns 1, undefined will be returned. So alert(fn()) equals alert(undefined)

  1. What is the result of executing the following code?
function fn(){
    var a = 0
    function innerFn(){
        a++;
        console.log(a)
    }
    return innerFn;
}
fn()()
var newFn = fn()
newFn()
newFn()
Copy the code

Closures implement private variables

  1. What is the result of executing the following code?
var a = 100
function fn(){
    alert(a)
    var a = 10
    return this.a
}
alert(fn())
Copy the code

C) undefined (100); c) undefined (100); c) undefined (100)

  1. What is the result of executing the following code?
Var arr1 = [1,2,3,4] var arr2 = arr1.concat() arr2.push(arr1.splice(1,0)) console.log(arr1,arr2)Copy the code

[] [1, 2, 3, 4,] concatenate an array. Concatenate an array. Splice takes three arguments, the first is the number of bits to start from, the second is the length of the cut, and the third is the new data to add. The function returns the cut part. Here splice intercepts 0 bits, so it returns an array [], so it pushes arr2, so arr2 is [1, 2, 3, 4, []]

  1. What is the result of executing the following code?
var arr = new Array(3)
arr[1] = 5
arr[2] = 12
console.log(arr[0])
Copy the code

When an Array is created using the new Array constructor, if a number is passed, an Array of the corresponding length is created. Each item in the Array is null and the value is undefined

  1. What is the result of executing the following code?
var a = 100;
console.log(a++);
console.log(++a);
Copy the code

The ++ operator, if preceded by the ++, executes the statement first. If ++ follows, the statement is run first, followed by the variable ++

  1. What is the result of executing the following code?
var a = ! (3 <= 1) var b = ("abc" == "abc") && ("cc" ! = "dd") var c = (4 >= 4) && (5 <= 2) var d = (2 < 3) || (3 < 2) console.log(a, b, c, d)Copy the code

True true false true 3<=1 returns false, and the opposite returns true followed by b. For the && operator, if the front expression returns true, then b returns true followed by C. Similarly, if the back expression returns true, then 5<=2 returns false. Namely to false and then d, c about | | operators, returns true if the previous expression, the result is the result of one of the first expression. Where d is true

  1. Which of the following address bar statements is correct?

The value obtained by a.window.location. search is between question mark and #, including question mark but not # B. C.window. Location is the same as window.location.href. D.window

  1. What is the result of executing the following code?
var str = 'abcdef'
str.length = 2
console.log(str)
Copy the code

Answer: abcdef

Pp. 130-121

  1. What is the result of executing the following code?
function fn(){
    return foo;
    foo = 1;
    function foo(){}
    var foo = 'abc'
}
console.log(typeof fn())
Copy the code

Function declarations are improved before function declarations are improved as a whole

  1. What is the result of executing the following code?
var x = 1;
var obj = {
    x:2,
    foo:{
        x:3,
        fn:function(){
            return this.x
        }
    }
}
var f = obj.foo.fn
console.log(f())
console.log(obj.foo.fn())
Copy the code

F (); f(); f(); While obj.foo.fn() executes, this in it points to foo, so print 3

  1. What is the result of executing the following code?
var x = 1;
var obj = {
    x: 2,
    foo: function () {
        setTimeout(function () {
            var x = 3;
            console.log(this.x)
        },1000)
    }
}
obj.foo()
Copy the code

SetTimeout: this refers to the window

124. What is the result of executing the following code?

var x = 1;
function fn() {
    this.x = 2;
    return x
}
var f = new fn();
console.log(f.x)
Copy the code

This executes the object and returns the object when it is generated by new. Note, however, that if the display returns a reference value, the result of new is that reference value

125. What is the result of executing the following code?

var fn = function foo(){}
console.log(typeof foo)
Copy the code

The function name is used only in the function. It doesn’t exist outside the function

  1. What is the result of executing the following code?
var a = 12;
if(true){
    console.log(a)
    let a = 2
}
Copy the code

4, Uncaught ReferenceError: Cannot access ‘a’ before initialization

  1. What is the result of executing the following code?
Var a = [1,2,3,4,1,2,3,4,5,6, 'a', undefined, null, false, true] var s = new Set (a) the console. The log (s.s considering)Copy the code

11. The result of a Set of data is to deduplicate the array passed in

  1. What is the result of executing the following code?
let x = 1;
function foo(y=x){
    let x = 2
    console.log(y)
}
foo()
Copy the code

Answer: 1.

  1. What is the result of executing the following code?
fn(1)
var a = 2
function fn(a){
    console.log(a)
}
Copy the code

Precompilation causes variables and functions to be promoted, so calling functions can be used before they are declared. The function is called by assigning the parameter A to 1, so it prints out local A, which is 1

  1. What is the result of executing the following code?
let x = 1
function f1(x,y=x){
    console.log(y)
}
function f2(y=x){
    let x = 0
    console.log(y)
}
f1(2)
f2()
Copy the code

1. F1 executes a value that passes x to 2, so y defaults to 2. When f2 is executed, y defaults to x, and x is 1. So print 2 and 1 respectively

Pp. 140-131

  1. The following attribute applies to both the input and textarea elements
A.readonly
B.disabled
Copy the code

For example, if the input type is checkbox, set readonly

  1. What is the result of executing the following code?
function F(){}
var f = new F
console.log(f.prototype)
Copy the code

1. Prototype is a function property, not an instance property

  1. What is the result of executing the following code?
if(false){
    var a = 12
    let b = 5
}
console.log(a)
console.log(b)
Copy the code

ReferenceError: undefined Uncaught ReferenceError: b is not defined Let declaration traversal has the concept of block-level scope that cannot be used outside.

  1. Find the difference set of two arrays, e.g. [1,2,3] and [2,3,4].
Let arr1 = [1,2,3,4,5] let arr2 = [4,5,6,7,8] let a = new Set(arr1) let b = new Set(arr2) let arr3 = new Set(arr1.filter(x=>! b.has(x))) let arr4 = new Set(arr2.filter(x=>! A.h as (x))) let the diff = [arr3,... arr4] the console. The log (diff) / /,2,3,6,7,8 [1]Copy the code
  1. What is the result of executing the following code?
Var [= 'a', b = a, c = 'c'] = [1, 2, undefined] the console. The log (a, b, c)Copy the code

A and b are assigned 1,2; C, on the other hand, is undefined if the variable is not assigned by default. Similarly, undefined means there is no assignment, so use the default ‘c’.

  1. After executing the following code, what is the order of contents in UL?
//HTML code <ul id='list'> <li>1</li> <li>2</li> <li>3</li> <ul> var list = document.getelementbyId ('list') var li =  list.querySelectorAll('li') list.replaceChild(li[2],li[1]) list.insertBefore(li[1],list.children[2])Copy the code

Answer: 1, 3, 2

  1. What is the result of executing the following code?
var a = 1;
function a(a){
    console.log(a)
    var a = 2
}
console.log(a)
a(3)
Copy the code

TypeError: a(3) is not a function. TypeError: a(3) is not a function

  1. What is the result of executing the following code
var name = 'Alice';
(function(){
    if(name === 'undefined'){
        var name = 'Tom'
        console.log(name)
    }else{
        console.log(name)
    }
})()
Copy the code

If the name of the function is declared in the if statement, the name will be precompiled and the value will be undefined, so print Tom

  1. What is the result of executing the following code
var name = 'Alice' (function(){ if(name === 'undefined'){ var name = 'Tom' console.log(name) }else{ console.log(name) } }) ()Copy the code

TypeError: “Alice” is not a function. TypeError: “Alice” is not a function. The result is incorrect

  1. What is the result of executing the following code?
var a = 1;
function fn(){
    a = 2;
    console.log(a);
    console.log(this.a);
    var a;
    console.log(a)
}
fn()
Copy the code

2, 1, 2

Pp. 150-141

  1. What is the result of executing the following code?
if (! 'a' in window){ var a = 1 } console.log(a)Copy the code

Precompilation raises the variable declaration a in the if statement to undefined

  1. What is the result of executing the following code?
var obj = {
    val: 2,
    del: function () {
        this.val *= 2;
        console.log(val)
    }
}
obj.del()
Copy the code

We know that obj.del() changes obj.val to 4, but we need to note that when val looks for a variable, it will find the declared val outside the scope, not on the obj object.

  1. What is the result of executing the following code?
var name = 'Alice';
var  obj = {
    name:'Tom',
    getName:function(){
        return function(){
            return this.name
        }
    }
}
console.log(obj.getName()())
Copy the code

This refers to obj. GetName () returns function. This refers to window and outputs the name of window

  1. What is the result of executing the following code?
var name = 'Alice';
var  obj = {
    name:'Tom',
    getName:function(){
        var self =  this;
        return function(){
            return self.name
        }
    }
}
console.log(obj.getName()())
Copy the code

Tom: Self saves this, so self points to obj

  1. What is the result of executing the following code?
var a = 1;
setTimeout(() => {
    a = 2;
}, 0);
console.log(a)
Copy the code

Answer: 1.

  1. What is the result of executing the following code
(function (){
    var  a = b = 1;
})()
console.log(typeof a === "undefined")
console.log(typeof b === "undefined")
Copy the code

[C] indicate that global variables, which are assigned to variables that are not declared, belong to window

  1. What is the result of executing the following code?
var a = (function(foo){
    return typeof foo.bar;
})({foo:{bar:1}})
Copy the code

{foo:{bar:1}, so foo.bar does not exist

  1. What is the result of executing the following code? .
function f(){
    return f;
}
console.log(new f() instanceof f)
Copy the code

New f() returns f instanceof f

  1. What is the result of executing the following code?
function A() { }
A.prototype.n = 1;
var b = new A();
A.prototype = {
    n:2,
    m:3
}
var c = new A()
console.log(b.n,b.m)
console.log(c.n,c.m)
Copy the code

Answer: 1 undefined 2 3

  1. What is the result of executing the following code?
console.log(false.toString()); console.log([1,[2],3].toString()); console.log(1.. toString()); console.log(2.toString())Copy the code

Answer: ‘false’ ‘1,2,3’ ‘1’ SyntaxError

Pp. 160-151

  1. What is the result of executing the following code?
console.log([]! = = [])Copy the code

There is no such thing as implicit conversion. = = and = = =

  1. What is the result of executing the following code?
var a = 1;
console.log(a++)
console.log(++a)
console.log(a)
Copy the code

The ++ operator follows the ++ operator, and the ++ operator follows the ++ operator

  1. What is the result of executing the following code?
var a = { n: 1 }
var b = a;
a.x = a = { n: 2 }
console.log(a.n, b.n)
console.log(a.x, b.x)
Copy the code

1 1 undefined {n:2}

  1. What is the result of executing the following code?
console.log(c);
var c;
function c(a){
    console.log(a)
    var a = 3;
    function a(){}
}
c(1)
Copy the code

答案 : function c(a){… } function a(){} function a(){} function a(){

  1. What is the result of executing the following code?
console.log(typeof a)
function a(){}
var a;
console.log(typeof a)
Copy the code

Function declarations are promoted first, and functions as a whole are promoted later

  1. What is the result of executing the following code?
var a;
var b = 'undefined'
console.log(typeof a,typeof b, typeof c)
Copy the code

1, undefined string undefined; B is assigned to string undefined, so the type is string; Undeclared variables are detected using Typeof and undefined is returned

  1. What is the result of executing the following code?
var obj = {n:1}
function fn(a){
    a.n = 2
}
fn(obj)
console.log(obj.n)
Copy the code

Answer: 2

  1. What is the result of executing the following code?
var x = 10;
function fn(){
    console.log(x)
}
function show(f){
    var x = 20;
    f()
}
show(fn)
Copy the code

Answer: 10

  1. What is the result of executing the following code?
Object.prototype.bar = 1;
var foo = {
    g: undefined
}
console.log(foo.bar)
console.log('bar' in foo)
console.log(foo.hasOwnProperty("bar"))
console.log(foo.hasOwnProperty('g'))
Copy the code

1 true false true

  1. What is the result of executing the following code?
Object.prototype.a = 1;
var obj = {
    b:2
}
for(var i in obj){
    console.log(obj[i])
}
Copy the code

In the for in loop, the variable obj’s own attribute key is used first, and then the attribute on the variable’s prototype is removed

Pp. 170-161

  1. What is the result of executing the following code?
function fn1() {
    return {
        a: 1
    }
}
function fn2() {
    return
    {
        b: 1
    }
}
console.log(fn1())
console.log(fn2())
Copy the code

{a:1} undefined: if the function returns a line with nothing, it will return nothing

  1. What is the result of executing the following code?
console.log((function () { return typeof arguments })())
Copy the code

[c] Function arguments are an array of classes

  1. What is the result of executing the following code?
console.log(Boolean(false))
console.log(Boolean(""))
console.log(Boolean(null))
console.log(Boolean('0'))
console.log(Boolean(0))
console.log(Boolean(NaN))
Copy the code

False false false false false

  1. What is the result of executing the following code?
var x = 1;
if(function fn(){}){
    x += typeof fn;
}
console.log(x)
Copy the code

Answer: 1 undefined

  1. What is the result of executing the following code?
console.log('b' + 'a' + +'a' + 'a')
Copy the code

A + (+”a”),+’a’ unary operator, resulting in NaN, so it becomes baNaN, and finally adds ‘a’.

  1. What is the result of executing the following code?
var obj = {
    a: 1,
    b: 2
}
Object.setPrototypeOf(obj, { c: 3 })
Object.defineProperty(obj,'d',{value:4,enumerable:false})
for (const key in obj) {
    console.log(key)
}
Copy the code

A) Enumerable B) enumerable C) forin D) enumerable c) Enumerable D) forin

  1. What is the result of executing the following code?
var x = 1;
var f = {
    x:2,
    getx:function(){
        return this.x;
    }
}
console.log(f.getx())
var xGetter = f.getx;
console.log(xGetter())
Copy the code

1. Who calls this? Who does this refer to

  1. What is the result of executing the following code?
var x = 1;
x += typeof fn;
console.log(x);
function fn() { }
Copy the code

Answer: 1 the function

  1. What is the result of executing the following code?
Var a = [1,2,3] console.log(a.join())Copy the code

The join method is used to join the elements of an array by the specified character

  1. What is the result of executing the following code?
var a = [1]
var b = ['2']
console.log(b - a)
Copy the code

If both sides of a minus sign are arrays, and both arrays have only one member, and the member is a number or a string of numbers, the numbers in the two arrays will be subtracted

Pp. 180-171

  1. What is the result of executing the following code?
var b = 1;
function outer(){
    var  b = 2
    function inner(){
        b++;
        var b = 3;
        console.log(b)
    }
    inner()
}
outer()
Copy the code

Answer: 3

  1. What is the result of executing the following code?
console.log(1 > 2 > 3)
console.log(1 < 2 < 3)
Copy the code

A. false b. true C. false D. false

  1. What is the result of executing the following code?
(function(){
    console.log(1)
    setTimeout(function(){
        console.log(2)
    },100)
    setTimeout(function(){
        console.log(3)
    },0)
    console.log(4)
}())
Copy the code

Answer: 1, 4, 3, 2

  1. What is the result of executing the following code?
function sum(x,y){ if(y ! = undefined){return x + y}else{return function(y){return x + y}} console.log(sum(1,2)) console.log(sum(1)(2))Copy the code

Answer: 3, 3

  1. When executing the following code, what is printed when used to click the “button” on the page?
for (var i = 0; i < 5; i++) { var btn = document.createElement('button'); Btn.appendchild (document.createTextNode(' button '+ I)); btn.addEventListener('click', function () { console.log(i); }); document.body.appendChild(btn) }Copy the code

Answer: Each button is clicked and a 5 is typed: the for loop is finished before the click, and by the time I is accessed, it has changed to a 5

  1. What is the result of executing the following code?
var length = 10;
function fn(){
    console.log(this.length)
}
var obj={
    length:5,
    method:function(fn){
        fn();
        arguments[0]();
    }
}
obj.method(fn,1)
Copy the code

2. When fn executes, this points to the window. Arguments.fn (); fn (); fn (); fn (); fn (); fn (); fn (); fn ()

  1. What is the result of executing the following code?
(function(x){
    return (function(y){
        console.log(x)
    }(2))
}(1))
Copy the code

Answer: 1.

  1. What is the result of executing the following code?
var a = {},
    b = {key:'b'},
    c = {key:'c'};
a[b] = 123;
a[b] = 456;
console.log(a[b])
Copy the code

A [B] is the same as a[B]. A [B] is the same as a[B]

  1. What is the result of executing the following code?
var obj = { foo: 'bar', fn: function () { var self = this; console.log(this.foo, self.foo); // (function () { console.log(this.foo, self.foo); //undefined bar }()); } } obj.fn();Copy the code

When obj. Fn executes, this in fn refers to the caller obj. When fn executes immediately, this in fn refers to the window

  1. Which of the following statements about cycles is true?

C. Three expressions in the for loop can be omitted, and semicolons can also be omitted. D. The while statement executes the body of the loop at least once

Pp. 190-181

  1. Which of the following is not an error type in JavaScript?

[A]. [B]. [C]

  1. What is the result of executing the following code?
function fn(){
    return;
}
console.log(fn())
Copy the code

The function returns undefined when there is no explicit return or nothing after the return

  1. What is the result of executing the following code?
var obj = {a:1}
console.log(obj && obj.a)
Copy the code

The && operator returns the result of the following expression when the value of the preceding expression is converted to a Boolean value true

  1. What is the result of executing the following code?
var x = 5, y = 12; console.log(x < 10 && y > 1) console.log(x == 5 || y == 5) console.log(! (x == y)) console.log(!! x == x)Copy the code

The basic use of logical operators

  1. How do you iterate over properties on an object that are not inherited from the stereotype?

Answer: You can iterate through all the properties through the for in loop and determine whether the properties are on the object or the prototype by using hasOwnProperty() on the object

Function fn (obj) {if (typeof obj = = = 'object') {/ / regardless of the array for (const key in obj) {if call (obj, (object. The hasOwnProperty. key)) { console.log(key) } } }else{ return } } fn({a:1})Copy the code
  1. Topic:
{a:[{id: XXX, parentId: XXX}], B :[{id: XXX, parentId: XXX}, {id: XXX, parentId: XXX} : {a:[{isHistory: 1}], b:[{isHistory: 1}, {isHistory: 1}]}Copy the code

Answer:

Function delId(obj) {if (typeof obj == "object" && (! Array. IsArray (obj))) {/ / traverse Object for each item in the (const key in obj) {if (Object. The hasOwnProperty. Call (obj, key)) { const item = obj[key]; If (array.isarray (item)) {for (const val of item.values()) {if (array.isarray (item)) { If (typeof val == "object"&&(! Array.isArray(val))) { console.log(val) // 1. Delete the id and parentId attributes of the object delete val.id; delete val.parentId; // 2. Add an isHstory property to the object val.isHistory = 1; }}}}}}} / / test code const obj = {a: [{id: 'XXX', parentId: 'XXX'}], b: [{id: 'XXX', parentId: 'XXX'}, {id: 'xxx', parentId: 'xxx' }] } delId(obj); console.log(obj); {a:[{isHistory: 1}], b:[{isHistory: 1}, {isHistory: 1}]}Copy the code
  1. What is wrong with the JS assignment operator description?

A. Assignment operators are ordered from left to right B.i=j=k=”hello” means to initialize all three variables to “hello” C. Assignment expressions are assigned from right to left. D. The value of an assignment expression is the value of the right-hand operand

  1. What is true about this (multiple choice)

A.this object cannot be used in arrow functions. A. ply and call can change the current object when the function is executed so that this points to another object C. Because of the dynamic nature of JS, the reference to this needs to be determined at runtime. His always refers to the current object

  1. What is wrong with the JS function statement?

A. Functions can be called through timers. B. Self-executing functions can form an independent scope. If a function returns no value, or calls a return statement with no arguments, undefined is returned

  1. What is the result of the following statement execution?
console.log('111'<'33')
console.log(1>=-Infinity)
console.log('100'+'200')
Copy the code

Answer: true true 100200

Pp. 200-191

  1. The following statement is true (multiple choice)

A. Math.max does not accept arrays when called directly B. For built-in objects, toString does not get the desired result, but [object Objcet] C. The length property in a function returns the number of parameters in the function

  1. What is the result of executing the following code?
function foo(a) {
    var b = a * 2
    function bar(c) {
        console.log(a,b,c)
    }
    bar(b*3)
}
foo(2)
Copy the code

Answer: 2, 4, 12

  1. What is the result of executing the following code?
function foo(str,a){
    eval(str);
    console.log(a,b)
}
var b = 2
foo('var b = 3',1)
Copy the code

Eval takes the string passed as a JS statement and executes it, changing the scope of the original function

  1. What is the result of executing the following code?
function foo(str){
    'use strict';
    eval(str);
    console.log(a);
}
foo('var a = 1')
Copy the code

In strict mode, the eval runtime has its own lexical scope, meaning that declarations within it cannot be modified

  1. What is the result of executing the following code?
var a = 1;
(function foo(){
    var a = 2;
    console.log(a)
})()
console.log(a)
Copy the code

Answer: 2, 1

  1. What is the result of executing the following code?
var a = 1;
(function foo(lobal){
    var a = 2;
    console.log(a)
    console.log(lobal.a)
})(window)
console.log(a)
Copy the code

Answer: 2, 1, 1

  1. Draw a 0.5px line. Okay?
height: 1px; The transform: scale (0.5);Copy the code
  1. Draw a triangle?
div{ width: 0; height: 0; border-width: 100px; border-style: solid; border-color: transparent #0099CC transparent transparent; transform: rotate(90deg); /* Rotate 90° clockwise */}Copy the code
  1. What is the result?
function foo(x) { var tmp = 3; return function (y) { alert(x + y + (++tmp)); } } var bar = foo(2); // bar is now a closure bar(10);Copy the code

Es6 usually uses let const block-level scope instead. The closure disadvantage, ie can cause memory leaks. Strictly speaking, it is the fault of IE, not the closure problem

  1. What is 0.1+0.2?

0.30000000000000004 解析 : 0.1 + 0.2! 1. There is a loss of accuracy in converting decimal to binary 2. There is also a loss of precision when binary floating-point numbers are evaluated against each other