javascript运算符

    科技2022-07-12  165

    javascript运算符

    Optional Chaining operator ?When processing a nested object, we usually run into an error if the property is not added. The optional chaining operator allows used to validate the deeply located property without having to check if each chain is valid. This is similar to the . operator , except instead of causing an error it will return undefined. With the function calls it return undefined it the function reference is not found.

    可选的链接运算符 ? 在处理嵌套对象时,如果未添加该属性,通常会遇到错误。 可选的链接运算符允许用于验证位于深处的属性,而不必检查每个链是否有效。 这类似于. 运算符,除了不会引起错误外,它会返回undefined 。 通过函数调用,它返回undefined ,找不到函数引用。

    const dataObj = { name: "Brad", details: { country: "USA", city: "New york", contactsInfo: { mobile: 123, email: "xyz@gmailcom" } } }console.log(dataObj?.details?.country);// USAconsole.log(dataObj?.details?.university);// undefined/**Accessing methods*/console.log(dataObj?.name?.pop())// undefined

    Instead of throwing an error for using pop() method on a string, this operator will handle error and return undefined . This operator is useful if you are accessing a dynamic value without having to check its type.

    不会在字符串上使用pop()方法引发错误,该运算符将处理错误并返回undefined 。 如果要访问动态值而不必检查其类型,则此运算符很有用。

    Nullish coalescing operator

    空位合并运算符

    ?? operator is a logical operator is similar to || logical OR operator . The logical OR operator returns its right side value if the left side operator has a falsy value '' | false | undefined | 0 whereas the ?? operator return the right side value only if the left side operand has null | undefined. This operator will be pretty much useful when assigning default value/returning value if the expected result is null | undefined.

    ?? 运算符是逻辑运算符,类似于|| 逻辑OR运算符。 如果左侧运算符的值是伪造的,则逻辑OR运算符将返回其右侧值'' | false | undefined | 0 '' | false | undefined | 0 '' | false | undefined | 0而?? 仅当左侧操作数为null | undefined运算符才返回右侧值null | undefined null | undefined 。 如果预期结果为null | undefined.则在分配默认值/返回值时,此运算符将非常有用null | undefined. null | undefined.

    const x = 0 || 42// 42const x = 0 ?? 42// 0?? operator only looks for null or undefined type const obj = {}obj.name = obj.name ?? 'Brad'console.log(obj.name)// Brad

    Nullish coalescing assignment operator

    空位合并分配运算符

    ??= operator is similar to the nullish coalescing operator ,in addition to that it can be used to assign value to the property or variable if the result is null |undefined . Initially the syntax might be confusing, it will be better to leave a comment for others to understand at first.

    ??=运算符类似于空值合并运算符,此外,如果结果为null |undefined ,它可以用于将值赋给属性或变量。 最初,语法可能会造成混淆,最好先留下评论让其他人理解。

    const obj = {}obj.name = obj.name ?? "Shelby"// console.log(obj.name) // Shelby/** Short hand for the above will be */obj.name ??= "Shelby"console.log(obj.name)// Shelby

    Ternary operator

    三元运算符

    ?: operator can be used as short hand for if..else statement if the code block is simple and one-liner. Nested ternary operator can be used but will reduce the readability of the code.

    ?:如果代码块简单且只有一行,则可以将if: if..else语句用作简写形式。 可以使用嵌套的三元运算符,但会降低代码的可读性。

    const name ="John"if(name){console.log(name)}else {console.log("Name not added")}//Short-hand expressionname ? console.log(name) : console.log("Name not added")// John

    Comma operator

    逗号运算符

    , operator fits in with a set of expression operators and it can be useful, but it has a tendency to be used in ways that confuse rather than clarify. Part of the reason for this is that it is a comma, and commas have other meanings in JavaScript. So to be 100% clear, the comma is only a comma operator when it acts on two expressions:

    , 运算符适合一组表达式运算符,虽然它很有用,但是它倾向于以一种混淆而不是澄清的方式使用。 造成这种情况的部分原因是它是逗号,而JavaScript中的逗号还有其他含义。 因此,要100%清楚,当逗号作用于两个表达式时,它只是一个逗号运算符:

    lefthand expression , righthand expression

    左手表达,右手表达

    It is used to execute two expressions sequentially. It evaluates each of its operands from left to right first and then value of the last operand will be returned as the result.

    它用于顺序执行两个表达式。 它首先从左到右评估每个操作数,然后将最后一个操作数的值作为结果返回。

    const num = (20, 30);console.log(num);// 30const data = num ? (num,num*20) : numconsole.log(num)// 600

    In this case , the comma operator is used to execute a expression where the num is multiplied and returned.

    在这种情况下,逗号运算符用于执行将num相乘并返回的表达式。

    https://github.com/microsoft/TypeScript/issues/37255

    https://github.com/microsoft/TypeScript/issues/37255

    翻译自: https://medium.com/@mohamedmudasir00/useful-javascript-operators-you-should-know-90148e639514

    javascript运算符

    相关资源:微信小程序源码-合集6.rar
    Processed: 0.013, SQL: 8