Preface:

Recently summarized some of their own problems encountered in the process of doing easy to error, with analysis, I hope to help you.Copy the code

1. Which of the following results is true()

A ‘foo’ == new function(){ return String(‘foo’); };

B ‘foo’ == new function(){ return new String(‘foo’); };

C [] == 0

D ! []

E ! 0

Click to see the answer

A B C E

Click here to see what’s involved

1. The new return values

2.String() differs from new String()

3. Implicit type conversion

Resolution:

A ‘foo’ == new function(){ return String(‘foo’); };

  1. String('foo')= >'foo'
  2. new functionreturnDetermines whether the returned value is a reference data type or returns an empty object if it is not{}, sonew function(){ return String('foo'); }return{}
  3. 'foo' == {}{}Implicit type conversion,{}callvalueOfMethod returns{}Object, judgment{}Not a basic datatype, continue to calltoString()Method, return'[object Object]'.
  4. 'foo' == '[object Object]'To return tofalse

B ‘foo’ == new function(){ return new String(‘foo’); };

  1. new String(‘foo’); Mandatory String Object.
  2. ‘foo’ == new String(‘foo’), new String(‘foo’) calls itself valueOf method, returns ‘foo’
  3. ‘foo’ == ‘foo’ returns true

C [] == 0

The reference data type is converted to String and then to number

  1. Arrays first call their own toString() method

[].toString(); = > ‘2. Use the Number (‘) to a digital Number (”) = > 0 3. 0 = = 0 = > true

D ! []

Implicitly converts [] to a Boolean value

  1. Boolean([]) => true
  2. ! true => false

E ! 0

  1. Boolean(0) => false
  2. ! false => true

Tips:

Follow the following principles when comparing ==

1. If one of the operands is a Boolean, it is converted to a value before comparing equality; 2. If one operand is a string and the other is a value, convert the string to a value before comparing; 3. If one operand is an object and the other is not, the valueOf() method of the object is called and the resulting primitive type values are compared according to the previous rules. 4. If one operand is NaN, the equality operator returns false regardless of what the other operand is; 5. If both operands are objects, compare whether they are the same object. The equality operator returns true if it refers to the same object; 6. Null and undefined cannot be converted to other values until equality is compared. 7. Null and undefined are equal.Copy the code

2. Which of the following events will be triggered before the page is onload?

A readystatechange

B pageshow

C beforeunload

D DOMContentLoaded

Click to see the answer

A. that B. that C. that D. that

Click here to see what’s involved

1. Rendering process

parsing

A readystatechange

Document has a readyState property that describes the state of the document, and the readyStatechange event is triggered when the readyState changes.

Loading: A document is being loaded

Interactive: The DOMContentLoaded event is also triggered when the document is loaded

Complete: The load event is also triggered when the document and imported images are loaded

B pageshow

The PagesHow event is triggered when the load event is completed

C beforeunload

Triggered when the DOM is uninstalled, when the browser refreshes the page, jumps to the page, or closes the page.

D DOMContentLoaded

This event is triggered when the DOM is parsed and the synchronous JS and CSS are loaded before the LOAD event.

Tips:

Page loading rendering simple process

HTML parsing to DOM, CSS parsing to CSSOM, combining CSS and DOM to generate a rendering tree, layout, and rendering, simple for rendering

1. The browser requests HTML

2. The browser obtains the HTML file

3. The browser parses HTML

4. Parse to the head tag. If js is synchronized, dom parsing will stop (continue after JS is downloaded and executed). If CSS is used, DOM parsing will not be affected (but DOM rendering will be affected).

5. Go to the Body TAB

  1. If you only have THE DOM, parse the DOM and merge it with the CSS tree into a rendering tree for rendering.
  2. If there is an external chain JS, if it is synchronous JS, js will be downloaded and executed. At this point, DOM parsing will be suspended, and parsing will continue after JS execution is completed. After parsing, DOM tree will be generated and rendered.
  3. If there are JS and CSS introduced, synchronous JS blocks DOM parsing, dom waits for JS download to complete, dom parsing also needs to wait for CSS download to complete, and then DOM and CSSOM are merged into a rendering tree and rendered.

6. When dom parsing is complete

Tips:

When browsers encounter external JS via SRC, they render the parsed DOM first

Ex. :

Introducing external JS, Var I = 1000000000 while(I >0){I --} console.log(" parsing done ") HTML <h1>Hello</h1> <script Type ="text/javascript" SRC ="test.js"></script> <h1>world</h1> <h1>Hello</h1> <script type="text/javascript"> var I = 1000000000 while(I >0){I --} Console. log(" parsing done ") </script> <h1>world</h1> Hello world is displayed after js executionCopy the code

3. The correct conclusions about this code are :()

var F=function(){};

Object.prototype.a=function(){};

Function.prototype.b=function(){};

var f=new F();
Copy the code

A f can take A, but not B

B, f can take a, B

C, F can go to B, but not to A

D, F can go to A, not b

Click to see the answer

Answer: A,

Click here to see what’s involved

1. The prototype

Resolution:

One dot F can access those

  1. fthe__proto__Refers to the F constructorprototype.
  2. Attribute lookups are done through the stereotype chain.
  3. f.__proto__ = F.prototype ,F.prototype.__proto__ = Object.prototype.Object.prototype.__proto__ = null.
  4. fAccess toa.

F can access those

  1. F.__proto__ = Function.prototype. So F can access B
  2. Function.prototype.__proto__= Object.prototype. So F can access A.
  3. Conclusion: F can access A and B.

4. In the following result, returnfalseThe is?

A [] == true

B !! []

C NaN == NaN

D null == undefined

Click to see the answer

A. that B. that C. that D. that

Click here to see what’s involved

1. Implicit type conversion

Resolution:

A [] == true

If one of the operands is a Boolean, it is converted to a value before comparing equality;

  1. [] == Number(true) => [] == 1
  2. [].toString() == 1 => ” == 1
  3. Number(”) == 1 => 0 == 1
  4. false

B !! []

  1. Boolean([]) => true
  2. ! true => false
  3. ! false => true

C NaN == NaN

If one operand is NaN, the equality operator returns false regardless of what the other operand is;

  1. false

D null == undefined

Null and undefined are equal.

  1. true

5. The following code returns:

Number(null);

A Null

B 0

C undefined

D 1

Click to see the answer

Answer: B

Click here to see what’s involved

1. Use of the Number function

Resolution:

  1. Number(null) => 0
  2. Number(undefined) => NaN
  3. Number(123a) => NaN

6. What is the return from a(10) given the following code? (a)

function a(a)
{
  a^=(1<<4)-1;
   return a;
}
Copy the code

A 5

B 10

C 15

D 16

Click to see the answer

Answer: A,

Click here to see what’s involved

1. Binary conversion

2. Left-shift operator

3. Xor operators

Resolution:

Involving xOR, binary conversion,

  1. a^=(1<<4)-1 => a = a ^ ((1<<4) -1);
  2. Take the argument a = 10 ^((1<<4) -1);
  3. ((1 < < 4) – 1)
    1. 1<<<4 converts to binary 10000
    2. 10000 converted to decimal => 2⁴ => 16
    3. ((1 < < 4) – 1) = > 15
  4. 10 ^ 15
    1. If 10 is converted to binary, go to 1010
    2. 15 Switch to binary => 1111
    3. 10 ^ 15 = > 101
    4. 101 => 10 base 2²+1 => 5
  5. Return to 5

Tips:

Binary to decimal:

Mod two, then reverse order, high zero

Ex. :

1. 10%2 = 0 2. 5%2 = 1 3. 2%2 = 0 4Copy the code

7. Assuming val is already declared, it can be defined as any value. The following JS code may output the result:

console.log('Value is ' + (val != '0') ? 'define' : 'undefine');
Copy the code

A Value is define

B Value is undefine

C define

D undefine

E Value is define 或者 Value is undefine

F define 或者 undefine

G All the other choices are possible

Click to see the answer

Answer: C

Click here to see what’s involved

1. Operator priority

+ + + + + + + + + + + + +

('Value is ' + (val != '0')) ? 'define' : 'undefine'
Copy the code

8. Run the following program and the final result of y and z is:

<script> var m= 1, j = k = 0; Function add(n) {return n = n+1; } y = add(m); function add(n) { return n = n + 3; } z = add(m); </script>Copy the code

A 2, 4

4 B, 4

2 C, 2

D to abnormal

Click to see the answer

Answer: B

Click here to see what’s involved

1. Function promotion

2. The value

Resolution:

  1. Functions with the same name will overwrite each other. Since functions are promoted, the function defined later overwrites the function defined before, so y and z call the second add function.
  2. Since m is a basic type, changing it inside the function does not affect external variables

The end:

In the wrong topic to find their own knowledge weak points, consolidate and improve their knowledge system, suggest collection, often look, avoid forgetting ~Copy the code