"JS Authoritative Guide Learning summary--4.13 operator"

Source: Internet
Author: User

I. typeof operator

A typeof is a unary operator placed before its individual operand, which can be of any type. The return value is a string representing the type of operand.

For example:

typeof x Return value

Undefined "undefined"

Null "Object"

True or false "Boolean"

Any digit or nan "number"

Arbitrary string "string"

"Function" of arbitrary functions

Any built-in object (non-function) "Object"

Arbitrary host objects are implemented by the compiler in their respective strings, but not "undefined", "boolean", "Number", "string"

The most common usage of typeof is written in an expression:

(typeof value== "string")? "'" + Value + "'": Value

Two. Delete operator

Delete is a unary operator that is used to delete object properties or array elements. Delete is used to do the delete operation, not to return a value.

var a=[1,2,3];

Delete A[2]; Delete the last array element

2 in a//= false: element 2 does not already exist in the array.

A.length//= 3 Note: The length of the array does not change, although the previous line of code removes the element, but the delete operation leaves a "hole" and does not actually modify the length of the array.

It is important to note that:

Deleting an attribute or deleting an array element is more than setting a undefined value. When a property is deleted, this property no longer exists.

Reading a non-existent property returns undefined, but you can use the in operator to detect whether the property exists in the object.

Not all properties can be deleted, some built-in core and client properties cannot be deleted, and variables declared by the user through the Var statement cannot be deleted. Similarly, functions and function parameters that are defined by a function statement cannot be deleted.

For example:

var o={x:1,y:2}; Defines a variable that is initialized to an object

Delete o.x; Delete an object property, return True

typeof o.x//attribute not present, return "undefined"

Delete o.x//deletes the nonexistent property and returns True.

Delete O//cannot delete a variable declared through VAR, returns false in strict mode, throws an exception

Delete 1; parameter is not an lvalue, returns True,

This.x=1//Define a property for the global object, no VAR is used here

Delete x; Attempting to delete it, returning true in non-strict mode throws an exception in strict mode, using "Delete this.x" instead

X Run-time error, no X defined

Three. void operator

Void is a unary operator that appears before the operand and can be of any type. This operator is not used frequently.

The operands are evaluated as usual, but the results are ignored and undefined is returned.

Because void ignores the value of the operand, void is used to make the program more semantic when the operand has side effects.

This operator is most commonly used in client URLs-------Javascript:url, where expressions with side effects can be written in URLs, while void allows the browser to not display the technical results of this expression.

For example:

<a href= "Javascript:void window.open (); > Open a new Window </a>

<a href= "javascript:void (0);" > Open a new Window </a>

"JS Authoritative Guide Learning summary--4.13 operator"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.