gpt4 book ai didi

javascript - 那里有好的 JS 速记引用吗?

转载 作者:IT王子 更新时间:2023-10-29 03:21:28 26 4
gpt4 key购买 nike

我想在我的常规编码习惯中加入任何速记技术,并且当我在压缩代码中看到它们时也能够阅读它们。

有人知道概述技术的引用页或文档吗?

编辑:我之前提到过压缩器,现在我很清楚压缩和高效的 JS 类型技术是两个几乎完全不同的概念。

最佳答案

更新 ECMAScript 2015 (ES6)好东西。请参阅底部。

最常见的条件简写是:

a = a || b     // if a is falsy use b as default
a || (a = b) // another version of assigning a default value
a = b ? c : d // if b then c else d
a != null // same as: (a !== null && a !== undefined) , but `a` has to be defined

用于创建对象和数组的对象字面量表示法:

obj = {
prop1: 5,
prop2: function () { ... },
...
}
arr = [1, 2, 3, "four", ...]

a = {} // instead of new Object()
b = [] // instead of new Array()
c = /.../ // instead of new RegExp()

内置类型(数字、字符串、日期、 bool 值)

// Increment/Decrement/Multiply/Divide
a += 5 // same as: a = a + 5
a++ // same as: a = a + 1

// Number and Date
a = 15e4 // 150000
a = ~~b // Math.floor(b) if b is always positive
a = b**3 // b * b * b
a = +new Date // new Date().getTime()
a = Date.now() // modern, preferred shorthand

// toString, toNumber, toBoolean
a = +"5" // a will be the number five (toNumber)
a = "" + 5 + 6 // "56" (toString)
a = !!"exists" // true (toBoolean)

变量声明:

var a, b, c // instead of var a; var b; var c;

字符串在索引处的字符:

"some text"[1] // instead of "some text".charAt(1);

ECMAScript 2015 (ES6) 标准简写

这些都是相对较新的添加,因此不要指望在浏览器中得到广泛支持。它们可能受到现代环境(例如:较新的 node.js)或转译器的支持。 “旧”版本当然会继续工作。

箭头函数

a.map(s => s.length)                    // new
a.map(function(s) { return s.length }) // old

休息参数

// new 
function(a, b, ...args) {
// ... use args as an array
}

// old
function f(a, b){
var args = Array.prototype.slice.call(arguments, f.length)
// ... use args as an array
}

默认参数值

function f(a, opts={}) { ... }                   // new
function f(a, opts) { opts = opts || {}; ... } // old

解构

var bag = [1, 2, 3]
var [a, b, c] = bag // new
var a = bag[0], b = bag[1], c = bag[2] // old

对象字面量中的方法定义

// new                  |        // old
var obj = { | var obj = {
method() { ... } | method: function() { ... }
}; | };

对象文字中的计算属性名称

// new                               |      // old
var obj = { | var obj = {
key1: 1, | key1: 5
['key' + 2]() { return 42 } | };
}; | obj['key' + 2] = function () { return 42 }

奖励:内置对象的新方法

// convert from array-like to real array
Array.from(document.querySelectorAll('*')) // new
Array.prototype.slice.call(document.querySelectorAll('*')) // old

'crazy'.includes('az') // new
'crazy'.indexOf('az') != -1 // old

'crazy'.startsWith('cr') // new (there's also endsWith)
'crazy'.indexOf('az') == 0 // old

'*'.repeat(n) // new
Array(n+1).join('*') // old

好处 2:箭头函数还使 self = this 捕获变得不必要

// new (notice the arrow)
function Timer(){
this.state = 0;
setInterval(() => this.state++, 1000); // `this` properly refers to our timer
}

// old
function Timer() {
var self = this; // needed to save a reference to capture `this`
self.state = 0;
setInterval(function () { self.state++ }, 1000); // used captured value in functions
}

关于类型的最后说明

小心使用隐式和隐藏类型转换和舍入,因为它会导致代码的可读性降低,并且其中一些不受 modern Javascript style guides 的欢迎。 .但即使是那些更晦涩的代码也有助于理解其他人的代码,阅读最小化的代码。

关于javascript - 那里有好的 JS 速记引用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3899495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com