Answer Analysis at the bottom
1, Q: What is the code the following result Why??
console.log({} == {});
2, Q: What would the following code output Why??
var x = 1,
y = z = 0;
function add(n) {
return n = n + 1;
}
y = add(x);
function add(n) {
return n = n + 3;
}
z = add(x);
console.log(x, y, z);
3, Q: What would the following code output Why??
function Test(a, b, c) {
var d = 1;
this.a = a;
this.b = b;
this.c = c;
function f() {
d++;
console.log(d);
}
this.g = f;
}
var test1 = new Test();
test1.g();
test1.g();
var test2 = new Test();
test2.g();
4, Q: What would the following code output Why??
var name = 'baozhuang';
name += 10;
var type = typeof name;
if (type.length === 6) {
type.text = 'string';
}
console.log(type.text);
5, Q: Which of the following can output 1,2,3,4,5 Why??
// 1
function fn1(x) {
console.log(arguments);
return x;
}
fn1(1, 2, 3, 4, 5);
// 2
function fn2(x) {
console.log(arguments);
return x;
}(1, 2, 3, 4, 5);
// 3
(function fn3(x) {
console.log(arguments);
return x;
}(1, 2, 3, 4, 5));
6, Q: What is the result output of the following code Why??
function fn(x, y, z) {
z = 10;
console.log(arguments[2]);
}
fn(1, 2, 3);
7, Q: What is the result output of the following code Why??
function fn1() {
fn2.apply(null, arguments);
}
function fn2() {
console.log(arguments)
}
fn1(1, 2, 3, 4, 5)
8, Q: The code below is the result true
of Which Why??
// 1
console.log(undefined == null);
// 2
console.log(undefined === null);
// 3
console.log(isNaN('100'));
// 4
console.log(parseInt('1a') == 1);
9, Q: What is the result output of the following code Why??
var a = '1';
function fn() {
var a = '2';
this.a = '3';
console.log(a);
}
fn();
new fn();
console.log(a);
10, Q: What is the result output of the following code Why??
var a = 5;
function fn() {
a = 0;
console.log(a);
console.log(this.a);
var a;
console.log(a);
}
fn();
new fn();
-
1. Answer:
false
Q: How does it equal?Resolution:
引用值对比的是地址,两个空对象创建了不同的存储地址。所以不相等。 创建一个空对象赋值给 obj1, obj2 == obj1 。
var obj1 = {}; obj2 = obj1; console.log(obj2 == obj1);
-
2, the answer:
1 4 4
Resolution:
预编译 global object 1 变量声明 2 方法定义 3 解释一行执行一行 。 在预编译时方检查提升到最顶部 add() 会被替换
-
3, the answer is:
2 3 2
Resolution:
构造函数实例化形成闭包, 所以 d 在一次实例化会保存。 二次实例化重新赋值
-
4, the answer is:
undefined
Analysis:
type.text 为 new String(type).text =“string” 包装类无法存储, 所以下面直接 delete 掉 。
Note:如果想要保存 type.text 可以再var type = new String(typeof name) 中获得一个对象, 这样下面就可以保存text属性了
-
5. Answer:
1 3
Resolution:
1 正常调用方法 2 函数声明后面不能跟立即执行符号会报错。如果括号里面填写内容会当成一个独立的表达式。不会报错但不执行 3 立即执行函数
-
6. Answer:
10
Resolution:
实参和赋值的形参是映射关系同时存储于堆内存的一个位置
-
7. Answer:
1, 2, 3, 4, 5
Resolution:
所有的函数执行都有一个 fn2.call() 的过程, apply也一样。this指向不改变,传入参数还是一样调用fn2()
-
8, the answer is:
1 4
Resolution:
undefined , null 都不等于0 ,但是undefined == null ,一定要记住。isNaN('100') 隐式类型转换为isNaN(100)。parseInt('1a') 从第一位检查是数字留下直到不是数字为止
-
9, the answer is:
2 2 3
Resolution:
fn() 内输出 a 的值,所以只能为 2 。 在调用fn()函数的时候this.a =3 修改了 window下的a 值 所以 window.a = 3
-
10, the answer is:
0 5 0 0 “undefined” 0
Resolution:
fn() 函数调用时, a = 0; 已经被赋值所以a 只能等于 0; this.a = window.a =5; new fn() 构造函数内 a=0不变;this.a 在构造函数内并没有定义所以是undefined