一.类型转换
var a = false + 1;
console.log(a); //1
var b = false == 1;
console.log(b);//false
if(typeof(a) && (-true) + (+undefined) + ''){
console.log('通过了');
}
// typeof(a) --> 'undefined'
// -true --> -1 +undefined --> NaN
// -1 + NaN-->NaN NaN + '' --> 'NaN'
if(1+5*'3' === 16){
console.log('通过了');
}
console.log(!!' ' + !!'' + !!false ||'未通过');//1
window.a || (window.a = '1');
//()优先级高,先给a赋值,然后进行判断,window.a=='1',为true返回
二.
var fn = (
function test1(){
return '1';
},
function test2(){
return '2';
}
)();
console.log(typeof(fn));//string
var a = 10;
if(function b(){}){//不是false,所以为true
a += typeof b;//(function b(){})函数声明被()包含生成表达式,b被忽略
}
console.log(a);//'10undefined'
三.
var name = 'ddd';
name += 10;
var type = typeof name;
//如果想要输出string var type = new String(typeof name);
if(type.length === 6){
type.text = 'string';
}
console.log(type.text);//undefined
var x = 1,
y = z = 0;
function add(n){
return n = n + 1;
}
y = add(x);
function add(n){//预编译,将前面的add覆盖
return n = n + 3;
}
z = add(x);
console.log(x,y,z);//1,4,4
function test(x,y,a){
a = 10;
console.log(arguments[2]);//10 映射关系
arguments[1] = 9;
console.log(y);//9
}
test(1,2,3);
四.
var name = '222';
var a = {
name:'111',
say:function(){
console.log(this.name);
}
}
var fun = a.say;
//var fun = fuunction(){console.log(this.name)} this指向window
fun();//222
a.say();//111 对象里面的方法,this指向a
var b = {
name:'333',
say:function(fun){
fun();
}
}
b.say(a.say);//222 this指向window
b.say=a.say;
// b.say=fuunction(){console.log(this.name)}
b.say();//333
function Foo(){
getName = function(){//未经过var的变量会提升到全局,会把之前的getName覆盖
console.log(1);
}
return this;
}
Foo.getName = function(){
console.log(2);
}
Foo.prototype.getName = function(){
console.log(3);
}
var getName = function(){
console.log(4);
}
function getName(){
console.log(5);
}
Foo.getName();//2
getName();//4
Foo().getName();//1
getName();//1
new Foo.getName();//2 .运算优先级高于new
new Foo().getName();//3 new Foo()先执行
new new Foo().getName();//3