分类 JavaScript 下的文章

根据Understanding ECMAScript 6then接受2个参数。

The then() method is present on all promises and takes two arguments.
The first argument is a function to call when the promise is fulfilled. Any additional data related to the asynchronous operation is passed to this fulfillment function. The second argument is a function to call when the promise is rejected. Similar to the fulfillment function, the rejection function is passed any additional data related to the rejection.

- 阅读剩余部分 -

摘抄自Understanding ECMAScript 6

  • No this, super, arguments, and new.target bindings - The value of this, super, arguments, and new.target inside of the function is by the closest containing nonarrow function. (super is covered in Chapter 4.)
  • Cannot be called with new - Arrow functions do not have a [[Construct]] method and therefore cannot be used as constructors. Arrow functions throw an error when used with new.
  • No prototype - since you can't use new on an arrow function, there's no need for a prototype. The prototype property of an arrow function doesn't exist.
  • Can't change this - The value of this inside of the function can't be changed. It remains the same throughout the entire lifecycle of the function.
  • No arguments object - Since arrow functions have no arguments binding, you must rely on named and rest parameters to access function arguments.
  • No duplicate named parameters - arrow functions cannot have duplicate named parameters in strict or nonstrict mode, as opposed to nonarrow functions that cannot have duplicate named parameters only in strict mode.

There are a few reasons for these differences. First and foremost, this binding is a common source of error in JavaScript. It's very easy to lose track of the this value inside a function, which can result in unintended program behavior, and arrow functions eliminate this confusion. Second, by limiting arrow functions to simply executing code with a single this value, JavaScript engines can more easily optimize these operations, unlike regular functions, which might be used as a constructor or otherwise modified.

The rest of the differences are also focused on reducing errors and ambiguities inside of arrow functions. By doing so, JavaScript engines are better able to optimize arrow function execution.

Note: Arrow functions also have a name property that follows the same rule as other functions.

Template Literals,也就是模板字面量,非常强大,对于操作字符串,非常有用,至少解决了ES5没有解决的3个问题:

  • Multiline strings A formal concept of multiline strings.
  • Basic string formatting The ability to substitute parts of the string for values contained in variables.
  • HTML escaping The ability to transform a string such that it is safe to insert into HTML.

作用域的英文是: scope。函数作用域:function scope;块级作用域:block scope

OK,ES5 只有全局作用域和函数作用域,没有块级作用域。那么,ES6有了一个新的关键字:let,这个关键字的核心是限定了变量仅在块级作用域里起作用。

- 阅读剩余部分 -