Syntax
Void expr
The void operator calculates the expression expr and returns undefined. Example:
> Void 0
Undefined
> Void (0)
Undefined
> Void 4 + 7 // because the void ratio + has a higher priority, this expression is parsed to (void 4) + 7
NaN
> Void (4 + 7)
Undefined
> Var x;
> X = 3;
3
> Void (x = 5 );
Undefined
> X
5
Void is an operator rather than a function. It cannot be redefined. If you customize a void function, an exception is thrown, as shown below:
> Function void (expr) {// an error is reported here.
Return undefined;
}
SyntaxError: Unexpected token void
Note: In Firefox, the above Code only reports errors in strict mode. The same applies to other keywords and function names that retain keywords as function declarations.
> Function void (expr) {// No error is reported, but no void function is actually declared
Return undefined;
}
> Function void (expr) {// an error is returned in strict mode.
"Use strict ";
Return undefined;
}
SyntaxError: redefining void is deprecated
> Function class (expr) {// class is also a reserved keyword
"Use strict ";
Return undefined;
}
SyntaxError: redefining class is deprecated
Three Functions of void.
The rest of this article describes the three purposes of the void OPERATOR:
- UseVoid 0 insteadUndefined
- Bookmarks
- Execute JavaScript code in the Link
Purpose 1: UseVoid 0ReplaceUndefined
Void 0 or vOid (0) is always equal to uNdefined, With only one exception:When undefined is assigned a value again:
> Function a (undefined) {return undefined;} // The undefined here is a local variable.
> A ("hello ")
'Hello'
> Undefined = "foo"; // The undefined is actually window. undefined.
> Console. log (undefined );
Foo
WhileVoidAssigned again:
> Function B () {var void = function () {}; return void (0 );}
Missing variable name
Function B () {var void = function () {}; return void (0 );}
................ ^
> Void = "foo ";
Syntax error
Void = "foo ";
... ^
Note: Generally, undefined is actually a window. undefined (the property name and value are the same, window. naN, window. infinity), which is a global variable. On the ES3 engine, this variable can be modified. in the ES5 engine, it is a read-only attribute of window and cannot be re-assigned. However, in non-strict mode, the assignment operation will silently fail, but in strict mode, an exception is thrown.
Firefox3.6 (ES3 engine is implemented only for some ES5 features)
> Undefined = true;
> Console. log (undefined );
True
Firefox18 (ES5 is fully implemented)
> (Function (){
Undefined = true; // No error is reported, but the value is not successfully assigned.
Console. log (undefined );
})()
Undefined
> (Function (){
"Use strict ";
Undefined = true; // strict mode. If you assign a value to a read-only attribute, an exception is thrown.
})()
TypeError: "undefined" is read-only
If you worry about the globalUndefined variables will be modified by others, Then useVoid 0. Otherwise, it is easier to understandUndefined, because not everyone who reads your code is familiarVoid Operator.
Note: The popular practice is to write all your code in a self-executed function expression. A partial undefined variable is used inside the function, this ensures that the value of the undefined variable is actually undefined.
// Under ES3 Engine
> Undefined = true;
> Console. log (undefined)
True
> (Function (undefined ){
Console. log (undefined) // undefined is a parameter, but the corresponding real parameter is not input. Therefore, its value is actually undefined.
})()
Undefined
Purpose 2: bookmarks
Bookmarklets is a URI that can execute JavaScript code. If the result returned by a bookmarklet is notUndefinedThe content displayed on the current page will be replaced by the returned value.Void Operator. [The Webkit kernel browser will not have such troubles]:
- Javascript: 3 + 4Will replace the content of the current page7.
- Javascript: void (3 + 4)The content of the current page is not changed.Void willHide expression3 + 4 Results.
Similar:
- Javascript: window. open ("http://www.whitehouse.gov /")The content of the current page is replaced.
- Javascript: void window. open ("http://www.whitehouse.gov /")The content of the current page is not changed.
More complex example: This bookmarklet can submit the URL of the current pageSubmit.example.com:
javascript:void window.open("http://submit.example.com/submit?"+encodeURIComponent(document.location.href))
This bookmarklet will not change the content of the current page, but will open the page in the new tag or new window.
Note: There are many popular Bookmarklet files on the internet, usually imported into another js file, such as traditional Chinese to simplified Chinese, such as shopping site price comparison.
Javascript: void (document. body. appendChild (document. createElement ("script"). src = "http://tongwen.openfoundry.org/NewTongWen/tools/bookmarklet_cn2.js ")
Purpose 3: execute JavaScript code in the Link
Although this method is not recommended, it is indeed feasible, such as the following code:
<A href = "javascript: void computeResult ()"> Compute </a>
If the computeResult () function returns undefined (or no return statement, undefined is also returned by default), nothing happens. however, if this function returns other values, you must add the void operator before the function to prevent it from changing the content of the current page. [Webkit kernel browsers do not have such troubles].
Note: The more common usage is javascript: void (0), which is used to block the default redirect behavior of a link.