1. What is the difference between undefined and null?
undefined means that the definition is not assigned
let a; a is not assigned, at this time the type of a is undefiend |
---|
null is defined and assigned, but the value is null
let b = null; At this time the type of a is null |
---|
2. When do you assign null to a variable?
Initial assignment, indicating that the object will be assigned
let a = null; |
---|
a={name: “jack”}; |
Before the end, make the object garbage (collected by the garbage collector)
let a = null | |
---|---|
a = {name:“zs”}; | |
a =null |
3. Strictly distinguish between variable type and data type?
type of data:
1. Basic type
2. Object type
Variable type
1. Basic type: save is the basic type of data
2. Reference type: save the address value (such as: ox123)
example:
let a = "abc";
"abc" is the basic type in the data type ;
variable a is the basic type in the variable type; saving is the basic type of data ("abc")
The same
let b = [1,2];
[1,2] is the object type in the data type . The
variable b is the reference type in the variable type , and it stores the address value
4. Prototype
Question 1:
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,c.b,c.m)
// 1,undefined,2,3
Question 2:
function F(){
}
Object.prototype.a = function(){
console.log('a()')
}
Function.prototype.b = function(){
console.log('b()')
}
var f = new F()
f.a()
f.b()
F.a()
F.b()
//a()
//报错
//a()
//b()
5. Variable declaration promotion and function declaration promotion
Question 1:
var a = 4;
function fn(){
console.log(a);
var a = 5;
}
fn();
What is the final value of a? // undefined variable declaration promotion
Variable declaration promotion
- Variables defined (declared) by var can be accessed before the statement is defined
- Value: underfined
Question 2:
fn();
function fn(){
console.log(123);
}
Functions can be accessed before they are defined
Function declaration promotion
- The function declared by function can be called directly before
- Value: function definition object
note:
fn();
var fn = function (){
console.log(123);
}
This is the promotion of variable declarations, through var declaration is the promotion of variable declarations
6. Execution context stack
Question 1:
console.log('global bengin:'+i);
var i = 1;
foo(1);
function foo(i){
if(i==4) return;
console.log('foo() begin:'+i);
foo(i+1);
console.log('foo() end:'+i);
}
console.log('global end:'+i);
-
What will be output in turn?
-
How many execution contexts are generated during the whole process?
5 = foo(1)+f00(2)+foo(3)+foo(4)+window
Question 2:
function a(){
}
var a;
console.log(typeof a);
result:
analysis:
Function promotion has a higher priority than variable promotion, which means that it will be executed later, and the variable promotion will be executed first, and then the function promotion will be executed, and it will not be overwritten by the variable declaration. a is function promotion instead of variable promotion
Question 3:
if(!(b in window)){
var b =1
}
console.log(b);
result:
Question 4:
var c =1
function c(c){
console.log(c);
}
c(2) // 报错
result:
analysis:
c(2) c is a variable and not a function. Function promotion has a higher priority than variable promotion. It is executed after the representative. The variable promotion is executed first, and then the function promotion is executed. It will not be overwritten by the variable declaration, but will be Overwrite after variable assignment. So a is a variable promotion, so c is a variable and cannot be called, so an error will be reported
7. Job domain chain
Question 1:
var x = 10;
function fn(){
console.log(x);
}
function show(f){
var x =20;
f();
}
show(fn);
result:
analysis:
There is no x variable in the scope of the fn function. If there is no variable, then look up (look for the global scope) and the
output x is 10;
Question 2:
在这里插入代码片 var fn = function(){
console.log(fn);
}
fn()
var obj = {
fn2: function(){
console.log(fn2);
}
}
obj.fn2()
result:
analysis:
fn is a method under the global window, which can be found,
But fn2 is a method in the obj range, which can be accessed through this.fn2
var fn = function(){
console.log(fn);
}
fn()
var obj = {
fn2: function(){
console.log(this.fn2);
}
}
obj.fn2()
8. Closure
Question 1:
function fn(a=0){
var num =223
var fn1 = function(b=1){
num+=b
console.log(num);
}
num+=a;
return fn1
}
var fn2 = fn()
fn(2)()
fn(3)()
fn()(2)
result:
analysis:
Every time fn() is called, a new closure is generated, and the value of the formal parameter is different
Question 2:
function fn(n,o){
console.log(o);
return {
fn:function(m) {
return fn(m,n)
}
}
}
var a = fn(0); a.fn(1),a.fn(2),a.fn(3)
var b = fn(0).fn(1).fn(2).fn(3);
var c = fn(0).fn(1); c.fn(2);c.fn(3)
result:
Question 3:
var name = "text window";
var obj = {
name: "my object",
getNameFunc: function(){
return function(){
return this.name;
}
}
}
console.log(obj.getNameFunc()());
var name2 = "text window";
var obj = {
name2: "my object",
getNameFunc: function(){
var that = this
return function(){
return that.name2;
}
}
}
console.log(obj.getNameFunc()());