1.Function doSomething(arg1){
Arg1 = arg1 || 10; //arg1默认值为10
}
2.使用map()函数方法来循环数组里的项目
var squares = [1,2,3,4].map(function (val) {
return val * val; // [1, 4, 9, 16]
});
3.使用switch/case语句而非一系列的if/else
unction getCategory(age) {
var category = "";
switch (true) {
case isNaN(age):
category = "not an age";
break;
case (age <= 20):
category = "Baby";
break;
default:
category = "Young";
break;
};
return category;
}
4.数组和对象都用 []、{}
1.NAMES_LIKE_THIS 常量用这种方式,使用这个标志防止被重写
5.命名规范 functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis, EnumNamesLikeThis, methodNamesLikeThis, CONSTANT_VALUES_LIKE_THIS, foo.namespaceNamesLikeThis.bar, and filenameslikethis.js.
6.私有的属性和方法使用_开头,可选的参数opt_开头
Arg1 = arg1 || 10; //arg1默认值为10
}
2.使用map()函数方法来循环数组里的项目
var squares = [1,2,3,4].map(function (val) {
return val * val; // [1, 4, 9, 16]
});
3.使用switch/case语句而非一系列的if/else
unction getCategory(age) {
var category = "";
switch (true) {
case isNaN(age):
category = "not an age";
break;
case (age <= 20):
category = "Baby";
break;
default:
category = "Young";
break;
};
return category;
}
4.数组和对象都用 []、{}
1.NAMES_LIKE_THIS 常量用这种方式,使用这个标志防止被重写
5.命名规范 functionNamesLikeThis, variableNamesLikeThis, ClassNamesLikeThis, EnumNamesLikeThis, methodNamesLikeThis, CONSTANT_VALUES_LIKE_THIS, foo.namespaceNamesLikeThis.bar, and filenameslikethis.js.
6.私有的属性和方法使用_开头,可选的参数opt_开头
7.replace(/\s/g, '')除去空格
8.我们都熟悉jquery $(selector) 来选择元素,现在javascript 也支持这个方法:
document.querySelector(".select") //返回查到的第一个值
document.querySelectorAll //返回查到的所有值
document.querySelectorAll 和 简写形式 $('.select') $$('.select') 等价