资讯专栏INFORMATION COLUMN

ES5-8 & Polyfilling & Transpilling

chavesgu / 1236人阅读

ES7 includes() method

Array.prototype.includes(target) determines whether an array includes a certain element, returning True or False as appropriate.

String.prototype.includes(targetString) determines whether one string may be found within another string, returning True or False as appropriate.

Exponential operator **

x**y produces the same result as Math.pow(x,y)

ES8 String padding

padStart(targetLength, targetString/optional) and padEnd(targetLength, targetString) pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start (left) or the end (right) of the current string. If the argument targetString is missing, it will pad " " as default.

Trailing comma in function

Trailing commas (sometimes called "final commas") can be useful when adding new elements, parameters, or properties to JavaScript code. If you want to add a new property, you can simply add a new line without modifying the previously last line if that line already uses a trailing comma. This makes version-control diffs cleaner and editing code might be less troublesome.

ECMAScript 2017 (ES8) allows trailing commas in function parameter lists. The functions with final commas in the argument list are equivalent to those without them.

var add = (num1, num2,) => num1 + num2;
add(1,2,) //3

//The trailing comma also works with method definitions for classes or objects:
class C {
  one(a,) {},
  two(a, b,) {},
}

var obj = {
  one(a,) {},
  two(a, b,) {},
};

//Function parameter definitions or function invocations only containing a comma will throw a SyntaxError. Furthermore, when using a rest parameters, trailing commas are not allowed:
function f(,) {} // SyntaxError: missing formal parameter
(,) => {};       // SyntaxError: expected expression, got ","
f(,)             // SyntaxError: expected expression, got ","

function f(...p,) {} // SyntaxError: parameter after rest parameter
(...p,) => {}        // SyntaxError: expected closing parenthesis, got ","
Trailling comma in literals, destructuring and Json
// Arrays
var arr = [
  1, 
  2, 
  3, 
];
//If more than one trailing comma is used, an elision (or hole) is produced. An array with holes is called sparse (a dense array has no holes). When iterating arrays for example with Array.prototype.forEach() or Array.prototype.map(), array holes are skipped.
var arr = [1, 2, 3,,,];
arr.length; // 5

// Objects
var object = { 
  foo: "bar", 
  baz: "qwerty",
  age: 42,
};

// Destructuring
// array destructuring with trailing comma
[a, b,] = [1, 2];
// object destructuring with trailing comma
var o = {
  p: 42, 
  q: true,
};
var {p, q,} = o;
// Again, when using a rest element, a SyntaxError will be thrown:
var [a, ...b,] = [1, 2, 3];
// SyntaxError: rest element may not have a trailing comma

//Trailing commas in objects were only introduced in ECMAScript 5. As JSON is based on JavaScript"s syntax prior to ES5, trailing commas are not allowed in JSON.
Object.values() and Object.entries()

The Object.values(object) method returns an array of a given object"s own enumerable property values, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

const object1 = {
  a: "somestring",
  b: 42,
  c: false
};

console.log(Object.values(object1));
// >> Array ["somestring", 42, false]

// Use previous method "object.key()":
Object.key(object1).forEach(key => {
    console.log(object1[key])
    })
// The Object.keys() method returns an array of a given object"s own property names, in the same order as we get with a normal loop.

The Object.entries(object) method returns an array of a given object"s own enumerable property [key, value] pairs, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

var users = {
    username1: "Camelia",
    username2: "Elio",
    username3: "Olivier"
    };
// [["username1", "Camelia"],["username2", "Elio"]["username3","Olivier"]]
    
Object.entries(users).map(entry => 
    [entry[1], entry[0].replace("username", "")])
// [["Camelia", "1"], ["Elio", "2"], ["Olivier", "3"]]
Async await
We cover these topics in an upcoming video in this section titled How Javascript Works as well as in the HTTP + AJAX + JSON + Asynchronous Javascript section.
Polyfilling & Transpilling

Polyfilling and transpilling are two technologies that allow bringing newer JS to older features.

Polyfilling

It refers to producing pieces of code equivalent to some new features, but able to run in older JS environment.
⚠️However, not all new features can be fully polyfilled. It"s better to use vetted set of polyfills such as those provided by ES5-Shime and ES6-Shim,

Transpilling

Newly added syntax cannot be polyfilled, they will throw an error in older JS engine as unrecognized/valid. The better solution is transpilling(transfoming+compiling), which converts the newer code to older code equivalents.

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/98456.html

相关文章

  • 怎么利用Python和C语言实现哈夫曼编码(霍夫曼编码)

      小编写这篇文章的主要目的是,教给大家怎么使用哈夫曼编码,也就是霍夫曼编码,并把具体的一些代码实例给大家贴了出来,希望可以为大家带来帮助。  一、用C语言实现哈夫曼编码  1、代码解释  (1)建立一个互相连接的表  我们在创建哈夫曼树的过程之中,要对互相之间的连接点进行增删查改,所以使用双向链路表格会更加的容易。'''C #include<stdlib.h>...

    89542767 评论0 收藏0
  • K8S ReplicationController模板

    在用户定义范围内,如果pod增多,则ReplicationController会终止额外的pod,如果减少,RC会创建新的pod,始终保持在定义范围。例如,RC会在Pod维护(例如内核升级)后在节点上重新创建新Pod。ReplicationController会替换由于某些原因而被删除或终止的pod,例如在节点故障或中断节点维护(例如内核升级)的情况下。因此,即使应用只需要一个pod,我们也建议使...

    白马啸西风 评论0 收藏0
  • Python完成GB文件格式编码序列编码序列Fasta文件类型

      本文关键给大家介绍了Python完成GB文件格式编码序列编码序列Fasta文件类型实例详细说明,感兴趣的小伙伴可以参考借鉴一下,希望可以有一定的帮助,祝愿大家尽可能发展,尽早工作上得到晋升  GB文件类型和FASTA文档详细介绍  在生物学中会有将GB文件格式编码序列编码序列成Fasta文件类型的需要,接下来我们运用python脚本制作来解决这些问题。  gb格式文档是GenBank的文档,用...

    89542767 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<