4個強大JavaScript運算符

你有沒有花一個下午的時間閱讀 Mozilla 文檔?如果有,你會發(fā)現(xiàn)網(wǎng)上有很多 JS 資料,這使我們很容易忽略那些更為基礎的 JS 運算符。
這些運算符不常見但很強大!在語法上看起來很相似,作用卻不一樣,一定要仔細閱讀。
null ?? 5 // => 5
3 ?? 5 // => 3var prevMoney = 1
var currMoney = 0
var noAccount = null
var futureMoney = -1
function moneyAmount(money) {
return money || `賬戶未開通`
}
console.log(moneyAmount(prevMoney)) // => 1
console.log(moneyAmount(currMoney)) // => 賬戶未開通
console.log(moneyAmount(noAccount)) // => 賬戶未開通
console.log(moneyAmount(futureMoney)) // => -1var currMoney = 0
var noAccount = null
function moneyAmount(money) {
return money ?? `賬戶未開通`
}
moneyAmount(currMoney) // => 0
moneyAmount(noAccount) // => `賬戶未開通`概括地說 ?? 運算符允許我們在忽略錯誤值(如 0 和空字符串)的同時指定默認值。
var x = null
var y = 5
console.log(x ??= y) // => 5
console.log(x = (x ?? y)) // => 5
function gameSettingsWithNullish(options) {
options.gameSpeed ??= 1
options.gameDiff ??= 'easy'
return options
}
function gameSettingsWithDefaultParams(gameSpeed=1, gameDiff='easy') {
return {gameSpeed, gameDiff}
}
gameSettingsWithNullish({gameSpeed: null, gameDiff: null}) // => {gameSpeed: 1, gameDiff: 'easy'}
gameSettingsWithDefaultParams(undefined, null) // => {gameSpeed: null, gameDiff: null}上述函數(shù)處理空值的方式有一個值得注意的區(qū)別。默認參數(shù)將用空參數(shù)(譯者注,這里的空參數(shù),只能是 undefined)覆蓋默認值,空賦值運算符將不會。默認參數(shù)和空賦值都不會覆蓋未定義的值。更多:
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Logical_nullish_assignment
var travelPlans = {
destination: 'DC',
monday: {
location: 'National Mall',
budget: 200
}
}
console.log(travelPlans.tuesday?.location) // => undefinedfunction addPlansWhenUndefined(plans, location, budget) {
if (plans.tuesday?.location == undefined) {
var newPlans = {
plans,
tuesday: {
location: location ?? "公園",
budget: budget ?? 200
},
}
} else {
newPlans ??= plans; // 只有 newPlans 是 undefined 時,才覆蓋
console.log("已安排計劃")
}
return newPlans
}
// 譯者注,對象 travelPlans 的初始值,來自上面一個例子
var newPlans = addPlansWhenUndefined(travelPlans, "Ford 劇院", null)
console.log(newPlans)
// => { plans:
// { destination: 'DC',
// monday: { location: '國家購物中心', budget: 200 } },
// tuesday: { location: 'Ford 劇院', budget: 200 } }
newPlans = addPlansWhenUndefined(newPlans, null, null)
// logs => 已安排計劃
// returns => newPlans object上面的例子包含了我們到目前為止所學的所有運算符。現(xiàn)在我們已經(jīng)創(chuàng)建了一個函數(shù),該函數(shù)將計劃添加到當前沒有嵌套屬性的對象 tuesday.location 中。我們還使用了非空運算符來提供默認值。此函數(shù)將錯誤地接受像“0”這樣的值作為有效參數(shù)。這意味著 budget 可以設置為零,沒有任何錯誤。
function checkCharge(charge) {
return (charge > 0) ? '可用' : '需充值'
}
console.log(checkCharge(20)) // => 可用
console.log(checkCharge(0)) // => 需充值var budget = 0
var transportion = (budget > 0) ? '火車' : '步行'
console.log(transportion) // => '步行'var x = 6
var x = (x !== null || x !== undefined) ? x : 3
console.log(x) // => 6function nullishAssignment(x,y) {
return (x == null || x == undefined) ? y : x
}
nullishAssignment(null, 8) // => 8
nullishAssignment(4, 8) // => 4function addPlansWhenUndefined(plans, location, budget) {
var newPlans =
plans.tuesday?.location === undefined
? {
plans,
tuesday: {
location: location ?? "公園",
budget: budget ?? 200
},
}
: console.log("已安排計劃");
newPlans ??= plans;
return newPlans;
}我們現(xiàn)在已經(jīng)學習了這些運算符的使用方法,在這里有更多關于這些運算符的知識。
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators
(譯者注:文中前三個運算符是 ES2020 的新特性,請勿直接用在生產(chǎn)環(huán)境, 可使用 webpack+babel 進行轉(zhuǎn)義,解決瀏覽器兼容性問題)
1.看到這里了就點個在看支持下吧,你的「點贊,在看」是我創(chuàng)作的動力。
2.關注公眾號
程序員成長指北,回復「1」加入高級前端交流群!「在這里有好多 前端 開發(fā)者,會討論 前端 Node 知識,互相學習」!3.也可添加微信【ikoala520】,一起成長。
“在看轉(zhuǎn)發(fā)”是最大的支持
