简介

console 中输入多行代码, shift + enter

only values has type in JS 只有值有类型,变量是没有类型的

typeof null === 'object' 历史遗留 bug

JS 数据类型

string
number
boolean
null and undefined
object
symbol (new to ES6)

Array 和 Function 其实也是 object,它们是 object 的子类型

转型

== 允许自动转型的情况下进行比较 checks for value equality with coercion allowed
=== 不允许自动转型的情况下进行比较 checks for value equality without allowing coercion

var a = 42;
var b = "foo";
a < b;      // false
a > b;      // false
a == b;     // false

如果两边有一个不是 string 或两边都不是 string, 两个值都会强转为 number 后再比较
b 转为 number 后是 NaN

Hosting 变量和函数提升
var a = 2;

foo();                  // works because `foo()`
                        // declaration is "hoisted"
function foo() {
    a = 3;

    console.log( a );   // 3

    var a;              // declaration is "hoisted"
                        // to the top of `foo()`
}
console.log( a );   // 2

Wherever a var appears inside a scope, that declaration is taken to belong to the entire scope and accessible everywhere throughout.

Strict Mode 应该始终开启
function foo() {
    "use strict";   // turn on strict mode
    a = 1;          // `var` missing, ReferenceError
}

foo();
在低版本的浏览器中使用高版本的特性2种方式
1. Polyfilling (垫片)

将高版本的特性在低版本中再实现一遍,如 ES6 中新添加了 Number.isNaN() 方法, 我们可以写一个像下面的垫片

if (!Number.isNaN) {
    Number.isNaN = function isNaN(x) {
        return x !== x;
    };
}

这样在所有浏览器中都可以使用 Number.isNaN 方法了。 一部分特性是可以通过 Polyfilling 实现的。

2. Transpiling

ES6、ES7 中新的语法无法通过 Polyfilling 部署到低版本中,这时可以通过第三方工具将 ES7、ES6 编译成 ES5 后再发布。
一般通过 Babel 或 Traceur 编译
Babel (https://babeljs.io) (formerly 6to5): Transpiles ES6+ into ES5
Traceur (https://github.com/google/traceur-compiler): Transpiles ES6, ES7, and beyond into ES5

对于 web 前端用 ES6、ES7 开发,打包时用 babel 编译回 ES5 是目前通用的做法。

新版的 Node.js 对 ES6 的支持达到了 99%, 所以 Node 是可以直接用 ES6 开发的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在2015年6月正...
    东东少将阅读 538评论 0 0
  • 参考ECMAScript6入门ECMAScript 6.0(以下简称ES6)是JavaScript语言的下一代标准...
    荞叶阅读 684评论 0 0
  • 对同一个问题,试着从不同的角度去理解,就会有不同的看法,也就会有不同的解决办法。 我想到了NLP 中所说的,凡事都...
    红雲说口才阅读 232评论 0 0
  • 活了三十多年。还真没体会过情人节的美妙滋味。如今,我更期待和同事一起出去聚聚餐,喝点酒。那样我会更快乐。
    如此这么阅读 145评论 0 0