Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the javascript special operator

Shulou Source: shulou.com Published: 2022-06-01 20:59:11 10月04日 Update

This article mainly explains "how to use the javascript special operator". Friends who are interested may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the javascript special operator.

Javascript special operators are: 1, the comma operator ",", which returns the value of the rightmost Operand; 2, the "delete" operator, which deletes a property of an object; 3, the "in" operator; 4, the "instanceof" operator; 5, the "typeof" operator; 6, the "void" operator.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Comma operator: returns the value of the rightmost Operand

The comma operator in javascript is a binary operator that executes the Operand on the left, then the Operand on the right, and finally returns the value of the Operand on the right.

A = b = 1 ~ c = 2 console.log (a); / return 1console.log (b); / / return 1console.log (c); / / return 2var x = (1, 2, 3); alert (x); / / 3

II. Delete operator: deletes an attribute of an object

The delete operator removes an attribute from an object or an element from an array.

Var obj = {x: 1, y: 2}; obj.x; / / 1delete obj.x;obj.x; / / undefined

Note:

1. Delete cannot delete the attributes of objects labeled false by configurable:

Var obj = {}; Object.defineProperty (obj,'x cards, {configurable: false, value: 1}); delete obj.x; / / falseobj.x; / / 1

2. Cannot be used to delete constants

Var a = 1 * * delete a; / / false

3. Cannot be used to delete function declarations

Function fun () {} delete fun; / / false

3. "in" operator: determines whether an attribute belongs to an object

The in operator requires that the Operand on the left is a string, and the Operand on the right is an array or object. If the value on the left is a property name of the object on the right, it returns true, otherwise it returns false.

Var obj = {name: 'person', age: 20};' name' in obj; / / true

4. Instanceof operator

The instanceof operator requires that the Operand on the left is an object, and the Operand on the right is the name of the object class, if the object on the left of the operator is

An instance of the class on the right returns true, otherwise it returns false.

Var d = new Date (); d instanceof Date; / / return trued instanceof Object; / / return trued instanceof Number; / / return false

5. Typeof operator

Typeof is a unary operator that precedes an Operand, which can be of any type. Its return value is a string that describes the type of Operand usually returned by typeof as follows: number,string,boolean,object,function,undefined

Typeof can enclose operands, similar to the use of a function

Var today=new Date (); typeof myFun / / return objecttypeof shape / / return stringtypeof size / / return number

6. Void operator

Void is a unary operator that can precede any type of Operand. This operator has a special purpose. It always discards operands.

Value and then return undefined. This operator is commonly used in javascript:URL on the client side, where you can evaluate the value of an expression that the browser does not display. Another use of void is to specifically generate undefined values

Var a = b = c = 2; d = void (a-= (b * = (c + = 5); console.log (a); / return-12console.log (b); / / return 14console.log (c); / / return 7console.log (d); / / return undefined here. I believe you have a better understanding of "how to use the javascript special operator". You might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Operation operator object right attribute Operand Special Type character string comma content function right Array Learning practical deeper function element Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information macOS Shulou Technology OPPO Reno Apple