12.面试题
function foo(){
bar.apply(null,arguments); //相当于把实参列表当做参数传给bar,并执行bar.
}
function bar(){
console.log(arguments)
}
foo(1,2,3)
typeof返回值有string,number,boolean,undefined,object,function然后null是归于object的,是占位符.
操作符","-->1,2会将操作符后面的值返回,这个就是返回2
//var num = (1 , 2);-->num = 2;
var f = (
function f(){
return "1";
},
function g(){
return 2;
}
)();
typeof f; //number
var x = 1;
if(function f(){}){ //这里是function函数声明提升以后归全局window所有,那么window上有这个,所以这个条件就是true,但是你直接打印f会报未定义的错,但是typeof f又是undefined
x += typeof f;
}
console.log(x); //1undefined
undefined == 0 //fales
undefined > 0 //fales
undefinde < 0 //fales
null > 0 //fales
null < 0 //fales
null == 0 //fales
undefined == null //true
undefined === null //fales
isNaN("100") //true
仿造NaN判断:
function imyIsNaN(num){
var ret = Number(num);
ret += "";
if(ret == 'NaN'){
return true;
}else{
return fales;
}
}
{} == {} //fales,引用值比对,比对的是地址,两房间,不一样
var obj = {};
obj1 = obj;
obj1 == obj; //true,引用的地址一样
obj1 === obj; //true,同一个地址
var foo = 123;
function print(){
this.foo = 234;
console.log(foo)
}
print(); //234,注意这里的this是指向window的,最后函数执行的时候改变了window的 foo的值
new print(); //123 这里this = Object.create(print.prototype) , 打印的是foo而不是this.foo
test()
new test()
var bar = { a: "2" }
function print(){
bar.a = "a";
Object.prototype.b = "b";
return function inner(){
console.log(bar.a);
console.log(bar.b);
}
}
print()();
// "a" "b"