调试方式:
1. F12 source 窗口
2. 添加debugger
变量提升:
函数声明和变量声明总是会被解释器悄悄地被"提升"到方法体的最顶部
没有被声明的变量将直接提升为global
=比较
==: 常规比较,忽略数据类型
var x = 10;
var y = "10";
if (x == y) //true
===: 恒等。数据类型和值 switch用恒等
var x = 10;
var y = "10";
if (x === y) //false
对象不能比较:
var x = new String("John"); // x is an object
var y = new String("John"); // y is an object
x==y //false
关键字
1. typeof
返回变量的数据类型
typeof [1,2,3,4] 返回 object, 数组是一种特殊的对象类型
{ps: 判断变量是obj or array
1. Array.isArray(array_name); //true
2.x.constructor.toString().indexOf("Array") > -1;
3.fruits instanceof Array;
4.
var o = { 'name':'lee' };
var a = ['reg','blue'];
var getDataType = function(o){
if(typeof o == 'object'){
if( typeof o.length == 'number' ){
return 'Array';
}else{
return 'Object';
}
}else{
return 'param is no object type';
}
};
}
Infinity is a number: typeOf Infinity returns number
typeof NaN = true
typeof function () = function
typeof null = object
2. Null(不包含任何值的数据类型)
什么都没有
null是一个只有一个值得特殊类型。表示一个空对象引用。
typeof(null) = object
null可以用来清空对象
3. Undefined (不包含任何值的数据类型)
(if (typeof myObj !=="undefined" && myObj !== null) )
一个没有设定值得变量
var person
变量的值如果不存在则该变量值为 undefined
//
var person;
document.getElementById("demo").innerHTML =
person + "<br>" + typeof person;
output:
undefined
undefined
var person = null;
document.getElementById("demo").innerHTML =
person + "<br>" + typeof person;
output:
null
object
/
typeof undefined // undefined
typeof null // object
null === undefined // false
null == undefined // true
/
NaN - Not a number
NaN is a JavaScript reserved word indicating that a value is not a number.
Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number):
var x = 100 / "Apple";
isNaN(x); //true
typeof NaN = true
/
var x = NaN;
var y = 5;
var z = x + y; // z will be NaN
//
NaN is a number, and typeof NaN returns number:
对象
访问方式: objectName.propertyName or objectName["propertyName"]
换行
1. break it after an operator.
2. break up a code line within a text string with a single backslash \
document.getElementById("demo").innerHTML = "Hello \
Dolly!";
The safest (but a little slower) way to break a long string is to use string addition:
document.getElementById("demo").innerHTML = "Hello" +
"Dolly!";
Boolean
1. Boolean(comparisions operator)
2. Boolean (real) = false
Boolean (undefined) =false
Boolean (null) =false
Boolean (false) =false
Boolean (NaN) =false
Boolean (undefined) = false
Boolean (empty string) = false
4.&&和||操作符
var val = 0 && ""; //0
var val = 0 || ""; //""
&&操作符总结:只要一个false就取false的值,都是true取后面,都是false取前面。
||操作符总结:只要一个是true就取true的值,都是true取前面,都是false取后面。