看了几道变量提拔的小题想分享给人人,都很简朴,我这里就不诠释缘由了。
我们先说一下预编译的历程
- 建立AO对象
- 找形参和变量声明,放入AO中,值为undefined
- 形参实参相统一
- 找函数声明,给AO中响应的的对象赋函数体
- 下面我们来看几个题:
1.
console.log(bar());//11 打印的是函数的返回值
function bar(){
foo = 100;
function foo(){}
var foo = 11;
return foo;
}
1. AO {
}
2. AO {
foo: undefined //变量声明
}
3. AO {
foo: undefined //无形参
}
4. AO {
foo: function foo(){}
}
2.
function bar(){
return foo;
foo = 100;
function foo(){}
var foo = 11;
}
console.log(bar());//fun
1. AO {
}
2. AO {
foo: undefined //变量声明
}
3. AO {
foo: undefined //无形参
}
4. AO {
foo: function foo(){}
}
3.
function test(){
console.log(b);//unde
if(a){
var b = 100;
}
c = 234;
console.log(c);//234
}
var a;
test();
a = 10;
console.log(c);//234
console.log(global);//unde
global = 100;
console.log(global);//100
function fn(){
console.log(global);//unde
global = 200;
console.log(global);//200
var global = 300;
console.log(global) //300
}
fn();
var global;
console.log(test);//fun
4.
function test(test){
console.log(test);//fun
var test = 234;
console.log(test);//234
function test(){}
}
test(1);
var test = 123;
console.log(test);//123
5.
function test(a,b){
console.log(a);//fun
console.log(b);//unde
var b = 234;
console.log(b);//234
a = 123;
console.log(a);//123
function a(){}
var a;
b = 28;
var b = function(){}
console.log(a);//123
console.log(b);//fn
}
test(1);
6.
function test(a,b){
console.log(a);//1
c = 0;
var c;
a = 3;
b = 2;
console.log(b);//2
function b(){}
function d(){}
console.log(b);//2
}
test(1);
7.
function fn(a){
console.log(a);//fun
var a = 123;
console.log(a);//123
function a(){}
console.log(a);//123
var b = function(){}
console.log(b);//fun
function d(){};
}
fn(1);
8.
console.log(a);//fun
a();//fun
var a = 3;
function a(){
console.log(a);
}
console.log(a);//3
a = 6;
a();//error a 不是一个函数 a = 6
9.
a(3);
var a = 1;
function a(a) {
console.log(a); //3
console.log(b); //fun
b = 1;
c = 2;
console.log(b); //1
var b = 5;
console.log(d); //fun
function b(){};
var a = 6;
console.log(c); // 2
if(false){
var d = 1;
c = 5;
}
console.log(c); //2
function d(){};
console.log(d); //fun
arguments[0] = 7;
console.log(a); //7
}
console.log(a); // 1
console.log(c); // 2
10.
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);};
Foo.getName();//2
getName();//4
Foo().getName();//1
new Foo.getName();//2
new Foo().getName();//3
new new Foo().getName();//3
实在上面一道题有关于变量提拔然则不是重点由于另有原型、继续、运算符优先级等知识点。
闲扯
- parseInt
console.log(parseInt('a', 16)); //10 console.log(parseInt(function(){}, 16)); //15 'f' console.log(parseInt(parseInt, 16)); //15 'f' console.log(parseInt(0.000002)); //0 console.log(parseInt(0.0000002)); //2 '2e-7' 大于即是7位才会用科学计数法示意 console.log(parseInt(false, 16)); //250 'fa' console.log(parseInt("113", 2)); //3 '11'
- 马上实行函数的写法
(function a() { console.log(1); })(); (function a() { console.log(1); }()); +function a() { console.log(1); }(); -function a() { console.log(1); }(); !function a() { console.log(1); }(); false || function a() { console.log(1); }(); true && function a() { console.log(1); }(); var a = function a() { console.log(1); }();
上面的代码都相称因而马上实行函数,实在主如果你把它变成一个表达式它就可以马上实行。也就是**只要表达式才被实行符号实行**。
+ 两个数组相加
```
let a = [1, 2],
b = [5, 3];
console.log(a + b); //1,25,3 相称于挪用了toString()
```
+ ==相称比较
我们都晓得```+0、-0、false、null、undefined、""、NaN```都是假值。
我们来看几个例子:
```
console.log(a + b); //1,25,3
console.log(false == ""); //true
console.log(false == []); //true
console.log(false == 0); //true
console.log(false == null); //false
console.log(false == undefined); //false
console.log(false == NaN); //false
console.log(false == {}); //false
```
```
console.log("" == []); //true
console.log("" == 0); //true
console.log("" == null); //false
console.log("" == undefined); //false
console.log("" == NaN); //false
console.log("" == {}); //false
```
```
console.log(0 == ""); //true
console.log(0 == []); //true
console.log(0 == null); //false
console.log(0 == undefined); //false
console.log(0 == NaN); //false
console.log(0 == {}); //false
```
另有几个比较难以明白的例子:
```
console.log([] == ![]); //true 由于![] = false, false == []是建立的。
2 == [1] //true
"" == [null] //true
0 == "/n" //true 由于'/n'被转换为0
```
+ [] + {}的题目
```
console.log([] + {}); // "[object Object]" [] = "", "" + {} 挪用{}的toString()
console.log({} + []); // 0
只不过是转换的前后递次差别
```