经过在程序最开始假如一个字符串字面量 use strict
,便可开启严格模式javascript
严格模式可让JS代码拥有更好的兼容性,更强的健壮性java
在严格模式下,从新声明 arguments
会报错,非严格模式则不会函数
'use strict'; function say() { var arguments = []; // Uncaught SyntaxError: Unexpected eval or arguments in strict mode }
若是链接两个不一样模式的JavaScript文件的话,若是是严格模式的文件放在开始的话,那么整个文件都是处于严格模式code
若是链接两个不一样模式的JavaScript文件的话,若是是非严格模式的文件放在开始的话,那么整个文件都是处于非严格模式对象
在文件中间使用use strict是没有做用的ip
为了同时使用严格和非严格模式的文件,能够经过当即调用函数达到分离做用域的目的作用域
(function() { // file1.js 使用了严格模式 'use strict'; function say() { } })(); (function() { // file2.js 使用非严格模式 function sayWithNoStrict() { var arguments = []; } })();
JavaScript中的数字都是 Number
类型,即双精度浮点数,JS中整数是双精度浮点数的一个子集字符串
JavaScript存在精度缺陷,如 0.1+0.2
,应该尽可能经过单位转换使用整数运算 1+2
编译器
注意 toString()
和 parseInt()
方法的参数string
console.log(typeof 36); // number console.log(typeof 36.36); // number console.log(typeof -36.36); // number console.log(0.1 * 3.6); // 0.36000000000000004 console.log(1 * 36); // 36 console.log(1 - 0.6); // 0.4 console.log(25 / 5); // 5 console.log(2.5 / 5); // 0.5 console.log(25 % 4); // 1 console.log(25 % 0.4); // 0.19999999999999862 console.log(8 | 1); // 9 console.log((10).toString(7)); // 13 参数表示返回值的进制数 console.log(parseInt('1010', 2)); // 10 第二个参数表示须要转换的数据的进制数,返回值为10进制 console.log(0.1 + 0.2); // 0.30000000000000004 console.log(0.1 + (0.2 + 0.3)); // 0.6 console.log((0.1 + 0.2) + 0.3); // 0.6000000000000001 console.log(((1 + 2) + 3) / 10); // 0.6 console.log(10 + (20 + 30)); // 60 console.log((10 + 20) + 30); // 60
String
+ Number
,Number
会被转为String
。
String
* Number
,String
会被转为Number
。
要小心 NaN
和 isNaN
的判断与使用
console.log(3 + true); // 4 console.log(1 + 1); // 2 console.log('hello' + ' world'); // hello world console.log(2 + '3'); // 23 console.log('2' + 3); // 23 console.log('2' + '3'); // 23 console.log(1 + '2' + 3); // 123 console.log(2 * '3'); // 6 console.log('8' | '1'); // 9 var x = NaN; console.log(x === x); // false var a = {}; console.log(a === a); // true var b = null; console.log(b === null); // true console.log(isNaN(NaN)); // true; console.log(isNaN({})); // true console.log(isNaN(undefined)); //true console.log(isNaN('foo')); // true 不能用isNaN来判断字符串 console.log(isNaN(1)); // false console.log(isNaN({valueOf: 'foo'})); // true console.log(isNaN({valueOf: function(){return 1}})); // false console.log('J' + {toString: function(){return 'S'}}); // JS console.log(1 + {valueOf: function(){return 2}}); // 3 var obj = { toString: function() { return 'obj'; }, valueOf: function() { return 1; } }; console.log(1 + obj); // 2 console.log('1' + obj); // 11 // bad function badPoint(x, y) { if(!x) { x = 1; } if(!y) { y = 1; } return { x: x, y: y } } // good function point(x, y) { if(typeof x === undefined || y === undefined) { return { x: x || 1, y: y || 1 } } return { x: x, y: y } } console.log(badPoint(0, 0)); // { x: 1, y: 1 } console.log(point(0, 0)); // { x: 0, y: 0 } console.log(point()); // { x: 1, y: 1 }
注意变量声明,原始对象和封装对象是不同的
var s = new String('hello'); console.log(s); // String {0: "h", 1: "e", 2: "l", 3: "l", 4: "o", length: 5, [[PrimitiveValue]]: "hello"} var str = s + ' world'; console.log(str); // hello world console.log(str[4]); // o console.log(typeof 'hello'); // string console.log(typeof s); // object var s1 = new String('hello'); var s2 = new String('hello'); console.log(s1 === s2); // false console.log(s1 == s2); // false console.log(str.toUpperCase()); // HELLO WORLD str.someProperty = 'some'; console.log(str.someProperty); // undefined s.someProperty = 'some'; console.log(s.someProperty); // 'some'
==
比较当使用 ==
操做符进行相等的比较操做的时候,若是它的两个参数的类型是不同的; 那么==会把它们先强制转换为相同类型参数,而后再进行比较。
使用 ===
代表你的比较不会涉及任何的隐形的类型转换。
当对不一样类型的数据进行比较的时候,你要首先把它们进行显示的类型转换。 而后再进行比较,这样会使你的程序更加清晰。
编译器仅在 {
标记以前,一行的结束和程序的结束处推导分号
仅在紧接着的标记不能被解析的时候推导分号
在以(,[,+,-或/字符开头的语句前决不能省略分号
当脚本链接的时候,应在脚本之间显式地插入分号
在return
, throw
, break
, continue
, ++
或 --
的参数以前决不能换行
var b = 12; function f() {} // 当@1和@2连在一块儿不可以解析的时候,分号才会自动插入 var a = b // @1 f() // @2 // 当@3和@4连在一块儿可以解析(虽然可能会解析失败)的时候,分号就不会自动插入了 var c = b // @3 (f()) // @4 // 在以`[`,`(`,`+`,`-`,`/`开头的语句前,永远不要省略分号 var d = 8; var e = 3 //此处的分号毫不能省略 +d console.log(e); // 11 // 当链接不一样的脚本的时候,要在不一样的脚本之间插入分号。 (function() { console.log('hello') })() ;(function() { //编写库函数的时候,经常在当即调用函数前添加分号,防止上一个文件最后一行无分号致使未知错误 console.log('world') })() // 否则就会解析出错 //(function() { // console.log('hello') //})()(function() { // console.log('world') //})() // 参数以前含有`return`,`throw`,`break`,`continue`,`++`,`--`的,参数与它们之间不要换行,不然会变为 `return;` function demoFunc() { return 1 } console.log(demoFunc()) // undefined 没有返回预期的结果 // 在循环语句的头部,分号不是用来当分隔符或者空语句使用的。 for(var i = 0; i < 3; i++) { console.log(i); } // 解析出错 //for(var i = 0 // i < 3 // i++) { // console.log(i); //}