Codewords are not easy, and helpful students hope to pay attention to my WeChat public account: Code program life, thank you! The code is self-used and fetched.
mind Mapping
1. Variable promotion
Interview questions
1. Ask the following output result
if( !("a" in window) ){
var a = 12
}
console.log(a) // undefined
Regardless of whether the conditions will be established to enhance the variables under global
var
equivalent towindow
set up a propertywindow.a = undefined
. So!("a" in window)
yesfalse
, the output isundefined
2. Ask the following output result
console.log(a)
var a = '林一一'
function fn(){
console.log(a)
var a = 12
}
fn()
console.log(a)
/* 输出
* undefined
* undefined
* 林一一
*/
With var,function
the upgrade because of the variables, initially a
for the undefined
assignment for the next global 林一一
. Private function with a scope inside var
the lift also make similar variables a = undefined
, the next step is a= 12
. Finally, the global output is林一一
3. The following output results
console.log(a)
var a = '林一一'
function fn(){
console.log(a)
a = 12
}
fn()
console.log(a)
/* 输出
* undefined
* 林一一
* 12
*/
No internal function variables improved, the internal function
a
is globala
, and finally perform the functiona
assigneda = 12
output result is obvious.
4. Seek the following output
console.log(a)
a = '林一一'
function fn(){
console.log(a)
a = 12
}
fn()
console.log(a)
/* 输出
* Uncaught ReferenceError: a is not defined
*/
Under global a
without var
, no variables upgrade. JS will look to their superiors scope, found window
in no property a
, so the reference error occurredReferenceError
5. Find the following output
var a = '林一一'
function fn(){
if(!a){
var a = 12
}
console.log(a)
}
fn()
/* 输出
* 12
*/
Regardless of whether the condition is established or not, the variable will be promoted
a= undefined
,if(!a) ==> if(!undefined) ==> true
and the output is 12
6. Find the following output
var a=12, b = 13, c = 14
function fn(a){
a = 0
var b = 0
c = 0
}
fn(a)
console.log(a)
console.log(b)
console.log(c)
/* 输出
* 12
* 13
* 0
*/
With the function parameter and var
a b
private variable, so the function a
, b
the wrong global a
, b
impact. The result is output 12
, 13
,0
Two, JS closure
Interview questions
1. Find the following output
var ary = [1, 2, 3, 4]
function fn(i){
return function(n){
console.log(n+ (i++))
}
}
var f = fn(10)
f(20) // 30
fn(20)(40) // 60
fn(30)(40) // 70
f(30) // 41
console.log(i) // Uncaught ReferenceError: i is not defined
This is a relatively simple problem, need to pay attention to a place that
n+ (i++)
isn
first andi
operationi
again from plus 1,()
will not work.
2. For a Teng interview question, the following code needs to realize 5 input buttons to bind the click click event in a loop. After the binding is completed, click the five buttons 1, 2, 3, 4, and 5 to output 0, 1, 2, and 3 respectively. , 4 five characters
- Can the following code be realized?
- Can't be achieved, what is the output effect below?
- How to modify to achieve the desired effect, explain the reason
<div id="btnBox">
<input type="button" value="button_1" />
<input type="button" value="button_2" />
<input type="button" value="button_3" />
<input type="button" value="button_4" />
<input type="button" value="button_5" />
</div>
<script type="text/javascript">
var btnBox = document.getElementById('btnBox'),
input = btnBox.getElementsByTagName('input')
var l = input.length
for(var i =0; i<l; i++){
input[i].onclick = function(){
alter(i);
}
}
</script>
1. Can not;
2. The results output is 5, because the event is binding异步的
when the binding event execution, the external cycle has ended, the variable usedi
is globali
, this timei=5
;
3.var
change Thelet
reasonlet
is that it has a block-level scope, and each block-level scope is private and does not interfere with each other. Or use closures to resolve, or customize event resolution.
Three, JS this
Interview questions
1.this points to
var name = '林一一'
var obj = {
name: '林二二',
prop: {
getName: function(){
return this.name
}
}
}
console.log(obj.prop.getName())
var a = obj.prop.getName
console.log(a())
/*
* undefined
* 林一一
/
2. Obtain the following output result with closure and this
var num = 10 // 60; 65
var obj = {
num: 20
}
obj.fn = (function (num){
this.num = num * 3
num++ // 21
return function(n){
this.num += n // 60 + 5 = 65;20 + 10 =30
num++ // 21 + 1 = 22;22 + 1 = 23
console.log(num)
}
})(obj.num)
var fn = obj.fn
fn(5) // 22
obj.fn(10) // 23
console.log(num, obj.num) // 65, 30
This question is a little bit more difficult. If you are familiar with the concept of superior scope and this point, it will be easier. Here only give a hint,
return
the higher the scope of the anonymous function is returned from the function is executedfn(5)
when the executionthis
pointwindow
,obj.fn(10)
executionthis
pointswindow
.
Four, JS constructor and prototype chain
Interview questions
1. A prototype and prototype chain interview question for a Teng, ask for the following output
function fun(){
this.a = 0
this.b = function(){
alert(this.a)
}
}
fun.prototype = {
b: function(){
this.a = 20
alert(this.a)
},
c: function (){
this.a = 30
alert(this.a)
}
}
var my_fun = new fun()
my_fun.b() // 0
my_fun.c() // 30
my_fun.b()
The scope of the function itself containing the attributeb
value of the output is 0;my_fun.c()
the attribute function itself is notc
, it will find upwardly through the prototype chainfun.prototype.c
, 30 is the output.
2. A Teng prototype redirects interview questions, ask for the following output
function Fn(){
var n = 10
this.m = 20
this.aa = function() {
console.log(this.m)
}
}
Fn.prototype.bb = function () {
console.log(this.n)
}
var f1 = new Fn
Fn.prototype = {
aa: function(){
console.log(this.m + 10)
}
}
var f2 = new Fn
console.log(f1.constructor) // ==> function Fn(){...}
console.log(f2.constructor) // ==> Object() { [native code] }
f1.bb() // undefined
f1.aa() // 20
f2.aa() // 20
f2.__proto__.aa() // NaN
f2.bb() // Uncaught TypeError: f2.bb is not a function
f1,f2中本没有 constructor
However, the constructor willprototype
find equivalent tof1.prototype.constructor
,f2
the prototype was redefined to a base classobject
;f2.__proto__.aa()
thethis
point is the prototypeprototype
, the prototype has no attributes andm
, thereforethis.m + 10 ==> undefined + 10 ==> NaN
.f2.bb()
Inf2
no attributesbb
, by__proto__
look up, and after the prototype is a prototype not redirect attributebb
, then up to the base classobject
is also no attributesbb
, thenf2.bb() ==>undefined()
, the errorTypeError
3. A factory's classic prototype interview questions, ask for the following output
function Foo() {
getName = function (){
console.log(1)
}
return this
}
Foo.getName = function () {
console.log(2)
}
Foo.prototype.getName = function(){
console.log(3)
}
var getName = function (){
console.log(4)
}
function getName() {
console.log(5)
}
// 1
Foo.getName()
//2
getName()
//3
Foo().getName();
//4
getName();
//5
new Foo.getName()
//6
new Foo().getName()
//7
new new Foo().getName()
/* 输出
* 2
* 4
* 1
* 1
* 2
* 3
* 3
*/
This question is a bit silly at first glance, but after every step of analysis, you can get an accurate answer. I try to understand this question, but you must have some pre-knowledge, such as variable promotion under the same name, prototype and prototype chain , This point, operator precedence, etc.
- In the lifting phase with variable
var
andfunction
will enhance the variables, the difference between the two isvar
only declared asundefined
not define,function
declare and define at the same time. The same namegetName
will first assign a function declaration and definition of push memory address, which is the output of the aboveconsole.log(5)
function address, the JS code is executed aftergetName
being reassigned to a new heap memory addressconsole.log(4)
.Foo.getName()
It is the call of the function as the object, the output in 1 is 2; - 2,
getName()
the output is 4, since thegetName()
reference is the addressconsole.log(4)
of; - 3
Foo().getName()
,Foo()
after performing in the globalgetName
reference again to the address changeconsole.log(1)
and returnsthis
to pointwindow
the output is 1; - 4
getName
is under the overall situation, so is the output naturally1
; - 5,
new Foo.getName()
function execution output2
; - 6
new Foo().getName() => new A.getName()
, prior tonew Foo()
re-instantiationA.getName()
calls, fornew Foo()
instance of the callgetName()
is to the prototypeprototype
on, the output of which is 3; - 7
new new Foo().getName() => new B.getName()
, firstlynew Foo()
instantiated againnew B.getName()
instantiated,new B.getName()
while also performingB.getName()
a method or instance outputB
method is the prototypeprototype
ofgetName
.
There is a WeChat mini program course design, complete design needs, contact personal QQ: 505417246
Pay attention to the following WeChat public account, you can receive WeChat applet, Vue, TypeScript, front-end, uni-app, full-stack, Nodejs, Python and other practical learning materials. The
latest and most complete front-end knowledge summary and project source code will be released to the WeChat public as soon as possible Number, please pay attention, thank you
After paying attention to the official account , reply to the front-end interview questions and receive a large number of front-end interview questions summary pdf data