Home > Web Front-end > JS Tutorial > Summary of usage of some non-mainstream operators in JavaScript

Summary of usage of some non-mainstream operators in JavaScript

伊谢尔伦
Release: 2017-07-19 10:38:52
Original
1471 people have browsed it

Javascript supports many other various operators.

i. Conditional operator (?:)

The conditional operator is the only ternary operator in JavaScript. Usually this operator is written as "?:". This operator has three operands. The first operand is before "?" and the second operand is between "?" and ":". The third operand is as early as after ":", for example

x > 0 ? x : -x; //Find the absolute value of x
The operand of the conditional operator can be of any type. The first operand is treated as a Boolean value. If it is true, the second operand is evaluated and the result is returned. Assignment If the first value operand is false, then the third operand is evaluated. and return the calculation result. The second and third operands always evaluate to one of them. It’s impossible to do both at the same time. In fact, the same effect can be achieved using if statements (5.4.1). The "?:" operator only provides a shorthand form. Here is a typical usage scenario of "?:", which determines whether a variable is defined. If it is defined, it is used. If it is not defined, a default value is used.

 grett = "hello" + (username ? username : "three");
Copy after login

is equivalent to the following code, but the above one is more concise

grett = "hello";
if (username)
   grett += username;
else
   grett + "three"
Copy after login

ii.typeof() operator

typeof is a unary operator, placed in a single In front of the operand, the operand can be of any type, and the return value represents a string representing the operation type.

   x             __ typeof x
            undefined     __ "undefined"
            null           __  "object"
            ture或false    __"boolean"
            任意数字或NaN     __ "Number"
            任意字符串                        __ "String"
            任意函数                            __ "function"
            任意内容对象(非函数)__ "object"
            任意宿主对象                    __ 由编译器各自实现的字符串,但不是"undefined" "boolean" "number" "string"
Copy after login

The most common usage of typeof is written in expressions like this

(typeof value == "string") ? "" + value + "":value;
The typeof operator is also very useful in the switch statement (5.4.3). It should be noted that the typeof operator can be used with parentheses. This makes typeof look like a function name instead of a keyword

typeof(i)

iii.delete operator

delete is a unary operator, It is used to delete properties of an object or elements of an array. Just like assignment, increment, decrement operators. Delete also has side effects. It is used for deletion operations. It is not used to return a value.

var o = {
                x: 1,
                y: 2
            }
            delete o.x;
            "x" in o; //=>false
            var a = [1, 2, 3];
            delete a[2]; // 删除数组中最后一个元素
            2 in a; //=> false 元素2已经在数组中不存在了
            a.length; //=>3,注意,数组长度并没有改变,尽管上一行删除了这个元素,但删除操作留下了一个洞。实际上并没有修改数组的长度,因此a的长度仍然为3
Copy after login

It should be noted that deleting an attribute or deleting an array element not only sets an undefined value. When an attribute is deleted, the attribute no longer exists. Reading a value that does not exist will return undefined. Regarding delete deletion and some situations in strict mode, those who need to learn need to experiment by themselves. Here are some examples.

var o = {x: 1,y: 2};
            delete o.x; //删除一个对象属性,返回true
            typeof o.x; //属性不存在,返回"undefined"
            delete o.x; //删除不存在的属性,返回true;
            delete o; //不能删除通过var关键字声明的变量,返回false
            delete 1; //参数不是一个左值。
            this.x = 1;// 给全局定义一个属性,这里没有使用var
            delete x ; //试图删除它,在非严格模式下返回true
                       //在严格模式下回抛出异常,这时使用"delete this.x"来代替
             x;        //运行时出错,没有定义x
Copy after login

iii.void operator.

void is a unary operator. Before the operand appears, the operand can be of any type. This operator is not commonly used: the operands are evaluated as usual, but the result is ignored and undefined is returned. Since void ignores the value of the operand, it makes more sense to program with void when the operand has side effects.

This is the most commonly used client url. Write an expression with side effects in the url, and void prevents the browser from displaying the result of this expression.

<a href="javascript:void window.open();">new</a>
Copy after login

iiii.Comma operator. (,)

The comma operator is a binary operator, and its operands can be of any type. It evaluates the left operand first and then the right operand.

 i = 0, j = 1, k = 2;
Copy after login

It is basically equivalent to the following code

i = 0; j = 1; k = 2;
The expression on the left side will always be evaluated, but the The result is ignored, that is, only if the left-hand expression has side effects, comma operations will be used to make the code smoother. The most commonly used scenario for the comma operator is in a for loop, which usually has multiple loop variables.

//for循环中的第一个逗号是var语句的一部分
             //第二个逗号是逗号运算符
             //它将两个表达式(i++和j++)放在一条(for循环中)语句中
            for (var i = 0, j = 10; i < j; i++, j--);
            console.log(i + j);
Copy after login

The above is the detailed content of Summary of usage of some non-mainstream operators in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template