/*
糟粕
*/
/*
==
JS有两组相等运算符:===和!== 以及 ==和!=
如果两个运算符类型一致且拥有相同的值,那么===返回true
而==和!=只有在运算数类型一致时,才会做出正确的判断,如果两个运算数类型不同,会进行强制类型转换
*/
console.log('' == 0);
console.log('' == false);
console.log(undefined == null);
console.log('' === 0);
console.log('' === false);
console.log(undefined === null);
//输出:
// true
// true
// true
// false
// false
// false
/*
with语句
JS提供了一个with语句,用来快捷的访问对象的属性,但结果不可预料,所以应当避免使用
*/
var withProperty = "some string",
withObj = {};
with (withObj) {
property = withProperty
}
console.log(withObj);
withObj.property = "no string";
with (withObj) {
property = withProperty
}
console.log(withObj);
withObj.withProperty = "other string";
with (withObj) {
property = withProperty
}
console.log(withObj);
//输出:
// Object {}
// Object {property: "some string"}
// Object {property: "other string", withProperty: "other string"}
//分析:
// 第一例,obj中没有with中的property属性,with中的赋值语句被直接忽略
// 第二例,obj中有了property属性,能够正常工作
// 第三例,obj中有withProperty属性,with中赋值语句直接读取了obj中的withProperty属性
/*
eval
eval函数传递一个字符串给JS编译器,并且执行结果
缺点
使得性能降低
减弱了应用程序的安全性
应当避免使用
Function构造器是eval的另一种形式,同样setTimeout和setInterval函数的字符串参数形式,也应当避免使用
*/
/*
continue
移除continue语句之后,性能都会得到改善
缺少块的语句
永远使用代码块
++--
这两个运算符鼓励一种不够严谨的编程风格
作为一条原则,建议不再使用
位运算符
JS有着与Java相同的一套位运算符
JS没有整数类型,只有双精度的浮点数
位操作符:它们的数字运算符先转换成整数,再执行运算,然后再转换回去,效率很低
*/
/*
function语句对比function表达式
function语句在解析时也会发生提升,这意味着无论function被放置在哪里,都会被移动到所在作用域的顶层
变量声明也会提升,变量声明提升为先,然后提升函数声明,所以函数声明会覆盖同名的变量声明
*/
risingFun();
function risingFun (){
console.log("In function");
}
//输出:
//In function
function risingSomething(){}
var risingSomething;
console.log(risingSomething);
//输出:
//function risingSomething(){}
/*
类型的包装对象
不要使用new Boolean、new Number或new String
尽量避免使用new Object和new Array,使用{}和[]来代替
new
JS的new运算符创建过程:
1. 创建一个继承于其运算数的原型的新对象
2. 调用该运算数
3. 把新创建的对象绑定给this
4. 运算数返回这个新创建的对象
最好的策略是别去使用new
*/
var SomeConstructor = function(){
console.log(this);
};
SomeConstructor.prototype.firPro = "firstProperty";
SomeConstructor.prototype.secPro = "secondProperty";
console.log(new SomeConstructor());
//输出:
// SomeConstructor {firPro: "firstProperty", secPro: "secondProperty"}
// SomeConstructor {firPro: "firstProperty", secPro: "secondProperty"}
/*
void
JS中void是一个运算符,接收一个运算数并返回undefined,没什么用,避免使用它
*/