js interview questions - See output is valid

Note:
log statement is the result of printing, direct display information;
dir statement to print the contents of all the properties and methods of display objects.

1. Object attribute name & stack memory

a point to an object, the object stored on the heap
Here Insert Picture Description

=> The difference between arrays and objects

Here Insert Picture Description
a [b] is stored [Object, Object]
A [C] is the same as the attribute name, so covering a [b] = 'training'
=> Object.prototype.toString / valueOf
Here Insert Picture Description

2. Symbol () to create a unique value

Results: a [b]: Everest
Here Insert Picture Description
=> Symbol own implementation of a function

3. Closure

Here Insert Picture Description
'. 4'
Alert results have toString ()
executed immediately function
function is a reference type, no parameters for storage in the stack, Test function
each time the function is executed, create a context (execution environment) performs

When defining the display function test, in which they are created in the execution context, that is, the higher the scope return a result (test function is executed immediately, it is immediately executed inside a function definition, the result is a function, so this function is executed immediately at the time of execution of the functions defined in), will go to a higher level of scope where to find
test (5) after the implementation, the context of the implementation of internal things are not occupied by other people, it will be destroyed
execution where i = 2 context can not be destroyed, because it is the scope of the right superior function, i of also preserved, closure
Here Insert Picture Description
a is rewritten GO represents the global variable
global constant a and b, the closure is not protected by the global contamination
Here Insert Picture Description

4. Object-Oriented

Here Insert Picture Description
Here Insert Picture Description
There are function, var, grammar ES5, so there will be JavaScript variable lift

  • JavaScript, functions and variables 声明will be promoted to the top most function (the current scope) of.
  • var statement in advance, function declarations advance copy +
  • So variables can be declared after use, that is, variables can be declared before use again.

function getName () {console.log (5 )} statement, but has a getName in the current scope, not re-statement, direct assignment
function is the object, is the prototype object in the heap
is a private function parameters, variables where the definition, where is higher scope
js operator priority table:. new Foo.getName () no arguments new; new Foo () getName new () with a parameter
instance invokes a method, the method must be a prototype
of the arrow function can not be new => difference function and a normal function of the arrows
Here Insert Picture Description

5. js asynchronous

EC function: Run Now function
main stack - EventQuene (micro tasks, macro task)
when the code went asynchronous, will go into the event queue, finished first main stack code execution, only to find an event queue
Here Insert Picture Description

promise, asyn, timers are asynchronous
browser is multi-threaded rendering & http request. .
js is single-threaded => browser only give js a thread to render
the event queue event queue
stacks in the main thread code execution is over, only to find an event queue (micro-priority task to perform [promise, asyn, await], then the macro [task timer, event binding, ajax]), on the main stack in execution

var a = ?
if(a == 1 && a == 2 && a == 3){
	console.log('条件成立');
}

= = Data type conversion
conversion rules:
After string becomes, then comparison .toString (): String = Object =
null = = undefined equal, but other values and comparison, it is no longer equal to the
NaN == NaN unequal
the rest are converted to digital

E.g. [10] = = 10
[10] .toString () -> "10" -> Number The ( "10") -> 10

=== absolutely equal

A method into a string of prototype rewrite toString () method, using a comparison rule ==
or valueOf ()
Here Insert Picture Description

2. The method of hijacking function
get obj.name: Trigger get ()
Set obj.name property: trigger the SET ()
Defineproperty getters interceptor can not obtain the current property again, so use ++ i, otherwise an error: stack overflow
Here Insert Picture Description
Here Insert Picture Description
a.shift deleted the first and return the item
Here Insert Picture Description

Published 149 original articles · won praise 5 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_26327971/article/details/105168753