1、
(function(){
var a=b=1 //var a; b=1;
})()
console.log(typeof a === "undefined");//typeof a => "undefined"
console.log(typeof b === "undefined");//typeof b => "number"
// true//false
2、
function foo1(){
return {
bar:"hello"
};
}
function foo2(){
return {
bar:"hello"
};
}
console.log(foo1());
console.log(foo2());
//{bar: "hello"}
//{bar: "hello"}
3、
var arr1='john'.split("");
var arr2=arr1.reverse();
var arr3="jones".split("");
arr2.push(arr3);
console.log(arr1.length+'-'+arr1.slice(-1));
console.log(arr2.length+'-'+arr2.slice(-1));
console.log(arr1);
console.log(arr2);
//5-j,o,n,e,s
//5-j,o,n,e,s
//["n", "h", "o", "j", Array(5)]
// ["j", "o", "n", "e", "s"]
//["n", "h", "o", "j", Array(5)]
// ["j", "o", "n", "e", "s"]
arr1===arr2 //true arr2是经过arr1变换得到,所以两个数组相等
console.log(0||1); //1
console.log(1||2); //1
console.log(0&&1); //0
console.log(1&&2); //2
5、
var a={},b={key:'b'},c={key:'c'};
a[b]=123;
a[c]=456;
console.log(a[b]); //456
console.log(a[c]); //456
console.log(a);
var obj={
_name:'tom',
greeting:function(){
return 'hello ' + this._name;
}
};
var greeting = obj.greeting;
console.log(obj.greeting()); //hello tom //this指向obj
console.log(greeting()); //hello undefined //this指向window window._name=undefined
console.log(sum(2,3)); //预期输出 5
console.log(sum(2)(3)); //预期输出 5
function sum(a,b){
if(b!==null && b!==undefined){
return a+b;
}else{
return function(c){
return a+c;
}
}
}
sum(2,3); //5
sum(2)(3); //5 //需要返回的是一个函数,并且传递一个参数