4 powerful JavaScript operators

In JS, the ?? operator is called a non-empty operator. If the first parameter is not null/undefined (Translator's Note: There are only two false values ​​here, but false values ​​in JS include: undefined undefined, empty object null, value 0, empty number NaN, boolean false, empty string' ', don't get confused), the first parameter will be returned, otherwise the second parameter will be returned. For example,
null ?? 5 // => 5
3 ?? 5 // => 3 When
setting a default value for a variable, the || logical OR operator was used before, for example,
var 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)) // => account is not opened console.log( moneyAmount(noAccount)) // => The account is not opened console.log(moneyAmount(futureMoney)) // => -1
账户未开通





Above we created the function moneyAmount, which returns the current user balance. We use the || operator to identify users who do not have an account. However, what does this mean when the user does not have an account? It is more accurate to treat no account as empty instead of 0, because bank accounts may have no (or negative) currency. In the above example, the || operator treats 0 as a false value, and should not include users with 0 USD accounts. Let's use the ?? non- null operator to solve this problem:
var currMoney = 0
var noAccount = null
function moneyAmount(money) { return money ?? } moneyAmount(currMoney) // => 0 moneyAmount(noAccount) // => In a nutshell?? operator allows us to specify default values ​​while ignoring error values ​​(such as 0 and empty strings). 2. ??= The null assignment operator ??= is also called the null assignment operator, which is related to the above non-null operator. Look at the connection between them: var x = null var y = 5 console.log(x ??= y) // => 5 console.log(x = (x ?? y)) // => 5
账户未开通


账户未开通







This assignment operator will only assign a value when the value is null or undefined. The above example emphasizes that this operator is essentially the syntactic sugar for null assignment (Translator's Note, similar syntactic sugar: a = a + b can be written as a += b ). Next, let us see the difference between this operator and the default parameter (Translator's Note, the default parameter is a new syntax introduced by ES6, only when the function parameter is undefined, set a default value for it):
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} chain judgment operator chain judgment operator?. Developers allowed Read property values ​​deeply nested in the object chain without having to verify every reference. When the reference is empty, the expression stops evaluating and returns undefined. such as:











var travelPlans = { destination:'DC', monday: { location:'National Mall', budget: 200 } } console.log(travelPlans.tuesday?.location) // => undefined Now, combine what we just learned Get up and add tuesday to your travel plan! function addPlansWhenUndefined(plans, location, budget) { if (plans.tuesday?.location == undefined) { var newPlans = { plans, tuesday: { location: location ?? "Park", budget: budget ?? 200 }, } } else { newPlans ??= plans; // Only when newPlans is undefined, will the console.log("plans scheduled") be overwritten } return newPlans }























// Translator's Note, the initial value of the object travelPlans is from the above example
var newPlans = addPlansWhenUndefined(travelPlans, “Ford Theater”, null)
console.log(newPlans)
// => {plans:
// {destination:'DC ',
// monday: {location:'National Mall', budget: 200} },
// tuesday: {location:'Ford Theater', budget: 200}}
newPlans = addPlansWhenUndefined(newPlans, null, null)
// logs => Scheduled plan
// returns => newPlans object The
above example contains all the operators we have learned so far. Now we have created a function that adds the plan to the tuesday.location object that currently has no nested attributes. We also used non-null operators to provide default values. This function will incorrectly accept a value like "0" as a valid parameter. This means that budget can be set to zero without any errors.
?: Ternary operator
?: Also called conditional operator, accepts three operands: condition? The expression to be executed when the condition is true: the expression to be executed when the condition is false. Actual effect:
function checkCharge(charge) { return (charge> 0)?'Available':'recharge required' }


console.log(checkCharge(20)) // => available
console.log(checkCharge(0)) // => recharge required
If you have written JS, you may have seen ternary operators. But, did you know that the ternary operator can be used for variable assignment?
var budget = 0
var transportion = (budget> 0)?'train':'walking'
console.log(transportion) // =>'walking'
we can even use it to implement null assignment behavior:
var x = 6
var x = (x !== null || x !== undefined)? x: 3
console.log(x) // => 6
Let us summarize this operation by creating a function:
function nullishAssignment(x,y) { return (x == null || x == undefined)? y: x } nullishAssignment(null, 8) // => 8 nullishAssignment(4, 8) // => 4



Guess you like

Origin blog.csdn.net/qq_45424679/article/details/114108294