基础类型:number、boolean、string、null、undefined
复杂类型:array、function、object
typeof
typeof 0 ==> "number"
typeof false ==> "boolean"
typeof "" ==> "string"
typeof null ==> "object"
typeof undefined ==> "undefined"
typeof [] ==> "object"
typeof function(){} ==> "function"
typeof {} ==> "object"
typeof new Number(0) ==> "object"
typeof new Boolean(false) ==> "object"
typeof new String("") ==> "object"
typeof new Array() ==> "object"
typeof new Function() ==> "function"
typeof new Object() ==> "object"
instanceof
object instanceof constructor
如果constructor.prototype.isPrototypeOf(object),则返回truecall&apply
call和apply都可以改变this的值;
call接受参数列表,apply接受参数数组。
function a(b, c) {
b == "first";
c == "second";
}
a.call({a: "b"}, "first", "second");
a.apply({a: "b"}, ["first", "second"]);
try-catch
使用throw抛出错误时,代码停止往下执行;
可以使用try-catch对异常进行捕获,使代码可以继续执行下去。遍历object的keys
for (var i in a) {
if (a.hasOwnProperty(i)) {
}
}
或者在V8引擎中:
Object.keys(a);
判断一个对象是数组
Array.isArray();
数组方法
遍历数组
[1, 2, 3].forEach(function (v) {
console.log(v);
});
过滤数组
[1, 2, 3].filter(function (v) {
return v < 3;
}); ==> [1, 2]
改变数组
[1, 2, 3].map(function (v) {
return v * 2;
}); ==> [2, 4, 6]
- 字符串方法
移除空格
" hello ".trim(); ==> "hello"
- bind
function a() {
this.hello == "world";
}
var b = a.bind({ hello: "world"});
b(); ==> true