介绍 ( Introduction )

ES2015 brings some heavy-hitting changes to the language, such as promises and generators. But not everything about the new standard is a landmark addition -- quite a few features are convenience methods that are quick to learn and fun to use.

ES2015对语言进行了一些重大更改,例如promise和generators 。 但是,关于新标准的所有内容并非都是具有里程碑意义的附加功能-便捷的方法中有很多功能可以快速学习和使用。

In this article, we'll take a look at a smattering of such goodies:

在本文中,我们将看一下其中的一些好处:

  • New collections: map, weakmap, set, and weakset
    新集合: mapweakmapsetweakset
  • Most of the new String methods; and
    大多数新的String方法 ; 和
  • Template literals.
    模板文字。

Let's start with the last of them; bottoms up.

让我们从最后一个开始。 一饮而尽。

Note: This is part 3 of the Better JavaScript series. You can see parts 1 and 2 here:

注意:这是Better JavaScript系列的第3部分。 您可以在此处看到第1部分和第2部分:

  • Better JavaScript with ES6, Part 1: Popular Features
    使用ES6更好JavaScript,第1部分:流行功能
  • Better JavaScript with ES6, Part 2: A Deep Dive into Classes
    使用ES6更好JavaScript,第2部分:深入研究类

模板文字 ( Template Literals )

Template literals scratch three itches, allowing you to:

模板文字刮擦了三个痕迹,使您可以:

  1. Evaluate JavaScript expressions inside of strings, called string interpolation.
    计算字符串内部JavaScript表达式,称为字符串插值
  2. Write multi-line strings, without having to concatenate strings or insert newline characters (\n).
    编写多行字符串,而不必连接字符串或插入换行符( \n )。
  3. Use "raw" strings -- strings in which backslash escapes are ignored, and interpreted literally.
    使用“原始”字符串-忽略反斜杠转义符并按字面意义进行解释的字符串。
"use strict";/* There are three major use cases for tempate literals: * String interpolation, multi-line strings, and raw strings.* ================================= */// ==================================
// 1. STRING INTERPOLATION :: Evaluate an expression -- /any/ expression -- inside of a string.
console.log(`1 + 1 =${1 + 1}`);// ==================================
// 2. MULTI-LINE STRINGS :: Write this:
let childe_roland =
`I saw them and I knew them all. And yet
Dauntless the slug-horn to my lips I set,
And blew “Childe Roland to the Dark Tower came.”`// . . . Instead of this:
child_roland =
'I saw them and I knew them all. And yet\n' +
'Dauntless the slug-horn to my lips I set,\n' +
'And blew “Childe Roland to the Dark Tower came.”';// ==================================
// 3. RAW STRINGS :: Prefixing with String.raw cause JavaScript to ignore backslash escapes.
// It'll still evaluate expressions wrapped in ${}, though.
const unescaped = String.raw`This${string()}doesn't contain a newline!\n`function string () { return "string"; }console.log(unescaped); // 'This string doesn't contain a newline!\n' -- Note that \n is printed literally// You can use template strings to create HTML templates similarly to the way
//   React uses JSX (Angular 2 uses them this way).
const template =
`
<div class="${getClass()}"><h1>Example</h1><p>I'm a pure JS & HTML template!</p>
</div>
`function getClass () {// Check application state, calculate a class based on that statereturn "some-stateful-class";
}console.log(template); // A bit bulky to copy the output here, so try it yourself!// Another common use case is printing variable names:
const user = { name : 'Joe' };console.log("User's name is " + user.name + "."); // A little cumbersome . . .
console.log(`User's name is${user.name}.`); // . . . A bit nicer.
  1. To use string interpolation, wrap your string with backticks instead of quotes, and wrap the expression whose result you want embedded in ${}.
    要使用字符串插值,请用反引号(而不是引号)包装您的字符串,并将要嵌入其结果的表达式包装在${}
  2. For multi-line strings, simply wrap your string in backticks, and break lines wherever you wish. JavaScript will insert a newline at the break.
    对于多行字符串,只需将字符串包装在反引号中,然后在需要的地方断开行即可。 JavaScript会在休息时插入换行符。
  3. To use raw strings, prefixe the template literal, still wrapped in backticks, with String.raw.
    要使用原始字符串,请使用String.raw在仍包裹在反引号中的模板文字加上前缀。

Template literals may be little more than sugar . . . But they're the sweetest.

模板文字可能只不过是糖而已。 。 。 但是它们是最甜的。

新的字符串方法 ( New String Methods )

ES2015 adds some additional methods to String, as well. These fall into two classes:

ES2015也向String添加了一些其他方法。 这些分为两类:

  1. General-use convenience methods; and
    通用便利方法; 和
  2. Methods for better unicode support.
    更好的unicode支持的方法。

We'll only cover the first class in this article, as the unicode-specific methods have fairly niche use cases. The MDN docs have a a full list of the new String methods, if you're curious.

我们将仅讨论本文的第一堂课,因为unicode专用方法具有相当不错的用例。 如果您好奇的话,MDN文档提供了新的String方法的完整列表 。

startsWith&endsWith ( startsWith & endsWith )

For starters, we now have String.prototype.startsWith. It's available on any string, and takes two arguments:

首先,我们现在有String.prototype.startsWith 。 它可用于任何字符串,并带有两个参数:

  1. A search string; and
    搜索字符串 ; 和
  2. An integer position, n. This is optional.
    整数位置n 。 这是可选的。

String.prototype.startsWith will check if the string you call it on starts with the search string, starting at the nth character of the string. If you don't pass a position, it starts from the beginning.

String.prototype.startsWith将检查您调用的字符串是否从搜索字符串开始,从搜索字符串第n个字符开始。 如果您没有通过某个职位,那么它将从头开始。

It returns true if your string starts with the search string, and false otherwise.

如果您的字符串以搜索字符串开头,则返回true否则返回false

"use strict";const contrived_example = "This is one impressively contrived example!";// does this string start with "This is one"?
console.log(contrived_example.startsWith("This is one")); // true// does this start with "is" at character 4?
console.log(contrived_example.startsWith("is", 4)); // false// does this start with "is" at character 5?
console.log(contrived_example.startsWith("is", 5)); // true

以。。结束 ( endsWith )

String.prototype.endsWith is similar: It takes a search string and a position, as well.

String.prototype.endsWith相似:它也需要一个搜索字符串和一个位置。

With String.prototype.endsWith, however, the position tells the function which character in the original string to treat as "last".

但是,使用String.prototype.endsWith ,该位置告诉函数原始字符串中的哪个字符被视为“最后一个”。

In other words, it'll chop off every character in your string after the nth, and check if that ends with the search string you passed.

换句话说,它会在第n次后你的字符串中的每个字符砍掉,并检查是否与搜索字符串结束你通过。

"use strict";const contrived_example = "This is one impressively contrived example!";console.log(contrived_example.endsWith("contrived example!")); // trueconsole.log(contrived_example.slice(0, 11)); // "This is one"
console.log(contrived_example.endsWith("one", 11)); // true// In general, passing a position is like doing this:
function substringEndsWith (string, search_string, position) {// Chop off the end of the stringconst substring = string.slice(0, position);// Check if the shortened string ends with the search stringreturn substring.endsWith(search_string);
}

包括 ( includes )

ES2015 also adds String.prototype.includes. You call it on a string, and pass it a search term. It returns true if the string contains the search term, and false otherwise.

ES2015还添加了String.prototype.includes 。 您在字符串上调用它,然后将其传递给搜索词。 如果字符串包含搜索项,则返回true否则返回false

"use strict";const contrived_example = "This is one impressively contrived example!";// does this string include the word impressively?
contrived_example.includes("impressively"); // true

Back in the days of cavemen, we had to do this:

早在穴居人时代,我们就必须这样做:

"use strict";
contrived_example.indexOf("impressively") !== -1 // true

Not much worse. But, String.prototype.includes is an improvement, in that it shields use from the leaky abstraction of equation truth to an arbitrary integer return value.

不会太糟。 但是, String.prototype.includes 一项改进,因为它可以防止使用从等式真值的泄漏抽象到任意整数返回值。

重复 ( repeat )

We've also got String.prototype.repeat. You can call this one on any string, and, like includes, it more or less does what its name implies.

我们还有String.prototype.repeat 。 您可以在任何字符串上调用它,并且像includes一样,它或多或少地执行其名称所暗示的功能。

It takes a single argument: An integer count. An example is clearer than an explanation, so here you go:

它只有一个参数:整数count 。 一个例子比一个解释更清楚,因此您可以进行以下操作:

const na = "na";console.log(na.repeat(5) + ", Batman!"); // 'nanananana, Batman!'

生的 ( raw )

Finally, we have String.raw, which we met briefly above.

最后,我们有String.raw ,我们在上面简短地遇到过。

If you prefix a template literal with String.raw, it won't evaluate escape sequences within the string:

如果您在模板文字前加上String.raw前缀,它将不会计算字符串中的转义序列:

/* Since the backslash alone means "escape", we need to double it to print*   one. Similarly, \n in a normal string is interpreted as "newline". *   */
console.log('This string \\ has fewer \\ backslashes \\ and \n breaks the line.');// Not so, with String.raw!
String.raw`This string \\ has too many \\ backslashes \\ and \n doesn't break the line.`

Unicode方法 ( Unicode Methods )

While we won't cover the rest of the new string methods, I'd be remiss if I didn't point you to a few must-reads on the topic.

尽管我们不会介绍其余的新字符串方法,但是如果我没有向您指出该主题的必读内容,那我将不为所动。

  • Dr Rauschmayer's introduction to Unicode in JavaScript;
    Rauschmayer博士对JavaScript中Unicode的介绍;
  • His discussion of ES2015's Unicode Support in Exploring ES6; and
    他在探索ES6中对ES2015的Unicode支持的讨论; 和
  • The Absolute Minimum Every Software Developer Needs to Know About Unicode.
    每个软件开发人员都需要了解Unicode的绝对最低要求 。

I just had to slip that last one in there somehow. Oldie but goodie.

我只需要以某种方式将最后一个滑进去。 虽然过时但好用。

Here are the docs for the missing string methods, just so you know what they are.

这是缺少字符串方法的文档,只是您知道它们是什么。

  • String.fromCodePoint & String.prototype.codePointAt;
    String.fromCodePoint和String.prototype.codePointAt ;
  • String.prototype.normalize; and
    String.prototype.normalize ; 和
  • Unicode point escapes.
    Unicode点转义 。

馆藏 ( Collections )

ES2015 brings us four new collection types:

ES2015为我们带来了四种新的收藏类型:

  1. Map and WeakMap
    地图和弱 地图
  2. Set, and WeakSet.
    Set和WeakSet 。

Proper Map and Set types are fantastically convenient, and weak variants, while somewhat exotic to the JavaScript landscape, are exciting additions to the language.

适当的Map和Set类型非常方便,而弱变体虽然对JavaScript领域有些许异国情调,但却是对该语言的激动人心的补充。

地图 ( Map )

A map is simply a key-value pair. The easiest way to think of this is by analogy with objects, whose property names are analogous to keys associated with a value.

映射只是一个键值对。 想到这一点的最简单方法是类比对象,其属性名称类似于与value关联的

"use strict";// We can think of foo as a key, and bar as a value.
const obj = { foo : 'bar' };// The foo 'key' of obj has value 'bar'
obj.foo === 'bar'; // true

The new Map type is conceptually similar, but lets you use arbitrary datatypes for keys -- not just strings and symbols -- and eliminates some of the many pitfalls associated with trying to use an object as a map.

新的Map类型在概念上相似,但是可以让您对键使用任意数据类型(而不仅仅是字符串和符号),并且消除了与尝试将对象用作地图相关的许多 陷阱 。

The following snippet demonstrates the Map API.

以下代码段演示了Map API。

"use strict";// Constructor
let scotch_inventory = new Map();// BASIC API METHODS
// Map.prototype.set (K, V) :: Create a key, K, and set its value to V.
scotch_inventory.set('Lagavulin 18', 2);
scotch_inventory.set('The Dalmore', 1);// You can also create a map from an array of 2-element arrays.
scotch_inventory = new Map([['Lagavulin 18', 2], ['The Dalmore', 1]]);// All maps have a size property. This tells you how many key-value pairs are stored within.
//   BE SURE TO USE 'size', NOT 'length', when you work with Map and Set.
console.log(scotch_inventory.size); // 2// Map.prototype.get(K) :: Return the value associated with the key, K. If the key doesn't exist, return undefined.
console.log(scotch_inventory.get('The Dalmore')); // 1
console.log(scotch_inventory.get('Glenfiddich 18')); // undefined// Map.prototype.has(K) :: Return true if map contains the key, K. Otherwise, return false.
console.log(scotch_inventory.has('The Dalmore')); // true
console.log(scotch_inventory.has('Glenfiddich 18')); // false// Map.prototype.delete(K) :: Remove the key, K, from the map. Return true if succesful, or false if K doesn't exist.
console.log(scotch_inventory.delete('The Dalmore')); // true -- breaks my heart// Map.prototype.clear() :: Remove all key-value pairs from the map.
scotch_inventory.clear();
console.log( scotch_inventory ); // Map {} -- long night// ITERATOR METHODS
// Maps provide a number of ways to loop through their keys and values.
//  Let's reset our inventory, and then explore.
scotch_inventory.set('Lagavulin 18', 1);
scotch_inventory.set('Glenfiddich 18', 1);/* Map.prototype.forEach(callback[, thisArg]) :: Execute a function, callback, on every key-value pair in the map. *   You can set the value of 'this' inside the callback by passing a thisArg, but that's optional and seldom necessary.*   Finally, note that the callback gets passed the VALUE and KEY, in that order. */
scotch_inventory.forEach(function (quantity, scotch) {console.log(`Excuse me while I sip this${scotch}.`);
});// Map.prototype.keys() :: Returns an iterator over the keys in the map.
const scotch_names = scotch_inventory.keys();
for (let name of scotch_names) {console.log(`We've got${name}in the cellar.`);
}// Map.prototype.values() :: Returns an iterator over the values of the map.
const quantities = scotch_inventory.values();
for (let quantity of quantities) {console.log(`I just drank${quantity}of . . . Uh . . . I forget`);
}// Map.prototype.entries() :: Returns an iterator over [key, value] pairs, provided as an array with two entries.
//   You'll often see [key, value] pairs referred to as "entries" when people talk about maps.
const entries = scotch_inventory.entries();
for (let entry of entries) {console.log(`I remember! I drank${entry[1]}bottle of${entry[0]}!`);
}

Maps are sweet. But objects are still useful for this kind of key-value record keeping. If all of the following hold, you might still want an object:

地图很贴心。 但是对象对于这种键值记录保持仍然有用。 如果满足以下所有条件,则可能仍需要一个对象:

  1. You know your key-value pairs when you write your code;
    您在编写代码时就知道键值对。
  2. You know you're probably not going to add or remove key-value pairs;
    您知道您可能不会添加或删除键值对;
  3. All of your keys are Strings or Symbols.
    您所有的键都是字符串或符号。

On the other hand, if any of the following are true, you probably want a map.

另一方面,如果满足以下任一条件 ,则可能需要一张地图。

  1. You need to iterate over the entries of the map -- this is surprisingly tricky to do with objects.
    您需要遍历地图的条目-对于对象而言,这非常棘手。
  2. You don't necessarily know the number or names of your keys when you write your code.
    编写代码时,您不一定知道键的编号或名称。
  3. You need complicated keys, like Objects or other Maps (!).
    您需要复杂的键,例如对象或其他地图(!)。

Iterating over an object you use as a map is possible, but tricky -- there are some nonobvious gotchas lurking in the shadows. Maps are much simpler to work with, and have the added advantage of consistency. Whereas object properties are iterated in random order, maps iterate over their entries in the order of insertion.

可以对您用作地图的对象进行迭代,但是很棘手-阴影中潜伏着一些不明显的陷阱。 地图使用起来更加简单,并且具有一致性的优势。 对象属性以随机顺序进行迭代,而按插入顺序在其条目上进行迭代映射

Similarly, adding arbitrary, dynamically named key-value pairs to an object is possible. But, tricky: If you ever need to iterate such a pseudo-map, you'll need to remember to update the number of entries manually, for instance.

类似地,将任意的,动态命名键-值对的一个对象是可能的 。 但是,有些棘手:例如,如果您需要迭代这样的伪映射,则需要记住手动更新条目数。

Finally, if you need keys that aren't Strings or Symbols, you don't have a choice but to use a Map.

最后,如果您需要的不是字符串或符号的键,则别无选择,只能使用Map。

These are just guidelines, but they're good rules of thumb.

这些只是指导原则,但是是很好的经验法则。

弱地图 ( WeakMap )

You may have heard of this nifty feature called a garbage collector, which periodically finds objects your program no longer needs and gets rid of them.

您可能已经听说过这种称为垃圾收集器的漂亮功能,该功能会定期查找程序不再需要的对象并清除它们。

To quote Dr Rauschmayer:

引用Rauschmayer博士的话 :

A WeakMap is a map that doesn't prevent its keys from being garbage-collected. That means that you can associate data with objects without having to worry about memory leaks.

WeakMap是一种不会阻止其键被垃圾回收的地图。 这意味着您可以将数据与对象相关联,而不必担心内存泄漏。

In other words, if your program loses all external references to the keys of a WeakMap, it can garbage-collect their values.

换句话说,如果你的程序丢失了一个WeakMap的钥匙所有外部引用,它可以收集垃圾的

For a good, albeit drastically simplified, use case, consider a SPA that displays items on a user's wishlists, with item descriptions and an image, which we consume as JSON returned by an API call.

对于一个很好的(尽管已大大简化的)用例,请考虑一个SPA,该SPA在用户的愿望清单上显示项目,项目说明和图像,我们将它们作为API调用返回的JSON使用。

It would make sense to cache those results to cut down the number of times we have to hit the server. We could use a Map for this:

缓存这些结果以减少我们必须打服务器的次数是有意义的。 我们可以为此使用地图:

"use strict";const cache = new Map();function put (element, result) {cache.set(element, result);
}function retrieve (element) {return cache.get(element);
}

. . . Which works, but potentially leaks memory.

。 。 。 哪个可行,但可能会泄漏内存。

Since this is a SPA, our users may want to navigate away from the wishlist view. That would make our "wishlist item" objects pretty useless, and eligible for garbage collection.

由于这是SPA,因此我们的用户可能希望远离心愿单视图。 这将使我们的“愿望清单项目”对象变得毫无用处,并且有资格进行垃圾回收。

Unfortunately, if you use a normal Map, you'll have to clear it yourself when those objects become unreachable.

不幸的是,如果您使用普通地图,则当这些对象变得不可访问时,必须自己清除它。

Using a WeakMap instead solves the problem for us:

使用WeakMap可以为我们解决问题:

"use strict";const cache = new WeakMap(); // No more memory leak!// The rest is the same . . .

This way, when the application loses all references to the unneeded elements, the garbage collector can recycle them automagically. Nifty.

这样,当应用程序丢失对不必要元素的所有引用时,垃圾收集器可以自动回收它们。 好漂亮

The API for WeakMap is similar to that of Map, with a few key differences:

WeakMap的API与Map的API类似,但有一些主要区别:

  1. You can only use Object keys in a WeakMap. That means no Strings, and no Symbols.
    您只能在WeakMap中使用对象键。 那意味着没有字符串,也没有符号。
  2. WeakMaps only have set, get, has, and delete methods -- that means you can't iterate over weak maps.
    WeakMaps仅具有setgethasdelete方法-这意味着您无法遍历弱地图
  3. WeakMaps don't have a size property.
    WeakMaps没有size属性。

The reason you can't iterate a WeakMap, or check its length, is because the garbage collector could run in the middle of your iteration: One moment, it'd be full. The next, empty.

您不能迭代WeakMap或检查其长度的原因是,因为垃圾收集器可以在迭代的中间运行:一会儿,它已满。 下一个,空的。

That sort of unpredictable behavior is precisely what the TC39 sought to avoid in forbidding iteration and size-checks on WeakMaps.

TC39试图避免在WeakMap上进行迭代和大小检查时要避免这种不可预测的行为。

For other use cases, check out the section on Use Cases for WeakMap, from Exploring ES6.

对于其他用例,请查看Exploring ES6中有关WeakMap的用例的部分。

( Set )

A Set is a collection that contains only unique values. In other words, each element of a set can appear only once.

集合是仅包含唯一值的集合。 换句话说,集合中的每个元素只能出现一次。

This is a useful data type if you need to keep track of objects that are inherently unique, such as the current users in a chat room.

如果您需要跟踪固有固有的对象(例如聊天室中的当前用户),则这是一种有用的数据类型。

Set and Map have almost identical APIs. The main difference is that Set doesn't have a set method, since it doesn't store key-value pairs. Everything is just about the same.

Set和Map具有几乎相同的API。 主要区别在于Set没有set方法,因为它不存储键值对。 一切都差不多。

"use strict";// Constructor
let scotch_collection = new Set();// BASIC API METHODS
// Set.prototype.add (O) :: Add an object, O, to the set.
scotch_collection.add('Lagavulin 18');
scotch_collection.add('The Dalmore');// You can also create a set from an array.
scotch_collection = new Set(['Lagavulin 18', 'The Dalmore']);// All sets have a length property. This tells you how many objects are stored.
//   BE SURE TO USE 'size', NOT 'length', when you work with Map and Set.
console.log(scotch_collection.size); // 2// Set.prototype.has(O) :: Return true if set contains the object, O. Otherwise, return false.
console.log(scotch_collection.has('The Dalmore')); // true
console.log(scotch_collection.has('Glenfiddich 18')); // false// Set.prototype.delete(O) :: Remove the object, O, from the set. Return true if successful; false if O isn't in the set.
scotch_collection.delete('The Dalmore'); // true -- breaks my heart// Set.prototype.clear() :: Remove all objects from the set.
scotch_collection.clear();
console.log( scotch_collection ); // Set {} -- long night./* ITERATOR METHODS* Sets provide a number of ways to loop through their keys and values. *  Let's reset our collection, and then explore. */
scotch_collection.add('Lagavulin 18');
scotch_collection.add('Glenfiddich 18');/* Set.prototype.forEach(callback[, thisArg]) :: Execute a function, callback,*  on every key-value pair in the set. You can set the value of 'this' inside *  the callback by passing a thisArg, but that's optional and seldom necessary. */
scotch_collection.forEach(function (scotch) {console.log(`Excuse me while I sip this${scotch}.`);
});// Set.prototype.values() :: Returns an iterator over the values of the set.
let scotch_names = scotch_collection.values();
for (let name of scotch_names) {console.log(`I just drank${name}. . . I think.`);
}// Set.prototype.keys() :: For sets, this is IDENTICAL to the values function.
scotch_names = scotch_collection.keys();
for (let name of scotch_names) {console.log(`I just drank${name}. . . I think.`);
}/* Set.prototype.entries() :: Returns an iterator over [value, value] pairs, *   provided as an array with two entries. This is a bit redundant, but it's*   done this way to maintain interoperability with the Map API. */
const entries = scotch_collection.entries();
for (let entry of entries) {console.log(`I got some${entry[0]}in my cup and more${entry[1]}in my flask!`);
}

弱集 ( WeakSet )

WeakSet is to Set as WeakMap is to Map. Like WeakMap:

WeakSet设置为WeakMap设置为地图。 像WeakMap:

  1. References to objects in a WeakSet are weakly-held.
    对WeakSet中的对象的引用是弱保留的。
  2. WeakSets do not have a size property.
    WeakSet没有size属性。
  3. You can't iterate over a WeakSet.
    您不能遍历WeakSet。

Use cases for weak sets don't abound, but there are a few. Domenic Denicola has called them "perfect for branding" -- that is, marking an object as satisfying some or other quality.

弱集的用例并没有很多,但有一些。 Domenic Denicola称它们为“完美的品牌塑造”-也就是说,将某个对象标记为满足某种或其他质量。

Here's the example he gave:

这是他给的例子:

/* The following example comes from an archived email thread on use cases for WeakSet.*    The text of the email, along with the rest of the thread, is available here:*      https://mail.mozilla.org/pipermail/es-discuss/2015-June/043027.html*/const foos = new WeakSet();class Foo {constructor() {foos.add(this);}method() {if (!foos.has(this)) {throw new TypeError("Foo.prototype.method called on an incompatible object!");}}
}

This is a lightweight technique way for preventing people from using method on any object that was not created by the Foo constructor.

这是一种轻量级的技术方法,可防止人们对Foo构造函数创建的任何对象使用method

Using a WeakSet has the advantage that it allows objects in foos to be garbage-collected when they become unreachable.

使用WeakSet的优点是,它允许在对象foos是垃圾收集,当他们变得无法访问。

结论 ( Conclusion )

In this article, we've taken a look at some of the sweeter sugar that ES2015 brings, from new convenience methods on String and template literals to proper Map and Set implementations.

在本文中,我们研究了ES2015带来的一些甜味,从关于String和模板文字的新便利方法到正确的Map和Set实现。

The String methods and template literals are easy to get started with. And, while you may not need to sling around weak sets anytime soon, I think Set and Map will grow on you pretty swiftly.

String方法和模板文字很容易上手。 而且,尽管您可能不需要很快就在弱集合上徘徊,但我认为Set和Map会Swift增长。

If you've got any questions, drop a line in the comments below, or hit me on Twitter (@PelekeS-- I'll get back to everyone individually.

如果您有任何疑问,请在下面的评论中添加一行,或者在Twitter上打我( @PelekeS-我将单独与大家联系。

翻译自: https://scotch.io/tutorials/better-javascript-with-es6-pt-iii-cool-collections-slicker-strings

使用ES6,Pt更好JavaScript。 III:酷收藏和闪烁的弦相关推荐

  1. 使用ES6写更好的JavaScript

    使用 ES6 写更好的 JavaScript part I:广受欢迎新特性 介绍 在ES2015规范敲定并且Node.js增添了大量的函数式子集的背景下,我们终于可以拍着胸脯说:未来就在眼前. - 我 ...

  2. 使用 ES6 编写更好的 JavaScript Part II:深入探究 [类]

    本文讲的是使用 ES6 编写更好的 JavaScript Part II:深入探究 [类], 在本文的开始,我们要说明一件事: 从本质上说,ES6 的 classes 主要是给创建老式构造函数提供了一 ...

  3. 2021-08-25556. 下一个更大元素 III

    556. 下一个更大元素 III 给你一个正整数 n ,请你找出符合条件的最小整数,其由重新排列 n 中存在的每位数字组成,并且其值大于 n .如果不存在这样的正整数,则返回 -1 . 注意 ,返回的 ...

  4. Java实现 LeetCode 556 下一个更大元素 III(数组的翻转)

    556. 下一个更大元素 III 给定一个32位正整数 n,你需要找到最小的32位整数,其与 n 中存在的位数完全相同,并且其值大于n.如果不存在这样的32位整数,则返回-1. 示例 1: 输入: 1 ...

  5. 前端优化方案-JavaScript 优化方案 收藏 此文于2010-06-04被推荐到CSDN首页

    前端优化方案-JavaScript 优化方案 收藏 此文于2010-06-04被推荐到CSDN首页 如何被推荐? Author: 李丽媛 Date: 2010/6/2 Email: lly219#gm ...

  6. javascript事件驱动框架 收藏

    javascript事件驱动框架 收藏 一个简单的事件驱动框架的演示: /*ControlDemo.js*/ //事件驱动框架(演示) function ControlDemo(page) {  // ...

  7. 常用:javascript字符串函数 收藏

    常用:javascript字符串函数 收藏 concat 将两个或多个字符的文本组合起来,返回一个新的字符串. var a = "hello"; var b = ",wo ...

  8. html中如何让字段闪烁,Javascript结合CSS实现边框闪烁提示

    Javascript结合CSS实现边框闪烁提示2017-09-01 23:12 当我们提交表单的时候,有些字段是必填的. 但是如果用户提交的时候偏偏不填写就提交,这时我们一般都会弹出一个框来提醒用户. ...

  9. ES6新特性:Javascript中的Reflect对象

    Reflect介绍: Reflect这个对象在我的node(v4.4.3)中还没有实现, babel(6.7.7)也没有实现 ,新版本的chrome是支持的, ff比较早就支持Proxy和Reflec ...

最新文章

  1. 在抛弃 MVP-Clean 后,我自主设计并开源了 Viabus 架构
  2. 硬核图解,再填猛将!
  3. C#-集合练习 107
  4. nginx php分离,nginx-php配置动静分离
  5. 雷神开机logo更改_九代酷睿i9加持的性能怪兽 雷神911黑武士Ⅱ评测
  6. Android 手机app缓存清理实现
  7. 开发者编程时应该围着“程序”转吗?
  8. IE浏览器下常见的CSS兼容问题
  9. 有哪些让人相见恨晚的Python库(一)
  10. D. Array Splitting
  11. Java中的锁 | JDK6 关于锁的优化
  12. 百度文库下载文档,没财富值,没下载卷也能下载
  13. 信息学奥赛一本通(c++):1125:矩阵乘法
  14. 穷举法破解密码-方法详解
  15. Java工程探讨2020-11-07
  16. 奇迹mu服务器架设全教程
  17. idea中更换java版本
  18. 基于飞浆ERNIE3.0百亿级大模型实现中文短文本分类任务
  19. vue下载pdf为空问题解决
  20. citrix vdi 服务器性能要求,Citrix测试VDI的最佳hypervisor

热门文章

  1. oracle数据库(表、视图、索引、事务)
  2. linux-c之函数(函数指针、函数传参、命令行参数)
  3. 【HTML】语义化标签
  4. google 的高级搜索
  5. 绿化草皮铺装施工与套用定额
  6. 编译时出现stripped of unavailable superclass
  7. gitlab展示CHANGELOG
  8. 定时自动关闭程序、打开网页和隐藏任务计划
  9. iOS导航控制器——UINavigationController使用详解
  10. form标签的enctype属性