JS 语法

2016-07-06  本文已影响124人  coolheadedY
<script defer src="script.js"></script>
<script async src="script.js"></script>
console.log(isNaN(NaN));//true
console.log(isNaN(10));//false
console.log(isNaN("10"));//false
console.log(isNaN("blue"));//true
console.log(isNaN(true));//false(布尔值可以转换为0和1)
Paste_Image.png
if (a != null) {
    //如果变量a定义了一个对象的引用就执行。
}
var oStringObject = new String("hello world"); 
 console.log(oStringObject instanceof String);   // 输出 "true"

参考:JavaScript instanceof 运算符深入剖析MDN

function isNumber(el){ 
      if (typeof(el) === number) {return "ture";}
      else {return "false";}
}
function isString(el){ 
      if (typeof(el) === string) {return "ture";}
      else {return "false";}
}
function isBoolean(el){ 
      if (typeof(el) === boolean) {return "ture";}
      else {return "false";}
}
function isFunction(el){ 
      if (typeof(el) === function) {return "ture";}
      else {return "false";}
}
var a = 2, 
         b = "jirengu", 
         c = false;
alert( isNumber(a) ); //true
alert( isString(a) ); //false
alert( isString(b) ); //true
alert( isBoolean(c) ); //true
alert( isFunction(a)); //false
alert( isFunction( isNumber ) ); //true
console.log(1+1); //2
console.log("2"+"4"); //24
console.log(2+"4"); //24
console.log(+new Date());//1467739918839
console.log(+"4");//4
var a = 1;
a+++a;//3
typeof a+2;//number2
var arr = [3,4,5];
for (i = 0; i < arr.length; i++ ) {
      console.log(arr[i] * arr[i]);
      // 输出 9, 16, 25 
}
var obj = { 
  name: 'hunger', 
  sex: 'male', 
  age: 28
}
for (var prop in obj) {
console.log(prop + ":" + obj[prop])
} 
// 输出 name: hunger, sex: male, age:28
console.log(a);//undefined 因为下一句var a = 1还没执行,相当于只进行了var a; a没有赋值就console.log(a)
var a = 1;
console.log(a);//1 变量a已经赋值为1
console.log(b);//error 这里的b并没有声明所以出现错误。
console.log(a);//undefined
var a = 1;
等价于下示
var a;
console.log(a);
a = 1;
//当全局变量赋值前被提升到最上面先进行了声明。
//JS在执行前会把所有的变量和函数提前声明

本博客版权归 本人和饥人谷所有,转载需说明来源

上一篇下一篇

猜你喜欢

热点阅读