void和undefined

undefined Undefined

has never been a JavaScript keyword or reserved word, which means we can modify the value of window.undefined.

Since undefined is frequently called as the operand of the comparison operation, it is disadvantageous to modify the value of undefined. Therefore, after the ES5 specification, window.undefined is defined as a non-writable and non-configurable property.

Common undefined:

If the variable is not assigned a value after it is declared, the variable will be automatically assigned as undefined
. Some formal parameters are defined in the function. If the incoming actual parameters are less than the predefined formal parameters, then some formal parameters will not match. The actual parameter will then be automatically assigned to
a function with no return value. It returns undefined by default . The void
operator

is followed by an expression. No matter what the content of the expression is, as long as it follows void, it will be called and executed. After completion, the void operator returns undefined:

void (expression)
In short, using the void operator has three uses:

generating undefined
to execute the function immediately Acting
as javascript: Protocol URI
generating undefined

Using void 0 to generate undefined has a long history. It not only reduces the time to find window.undefined on the prototype chain, but also avoids misuse of the modified undefined.

function checkLogin (loginName) {
    if (loginName === void 0) {
        console.log('Wrong!');
    }
}

checkLogin();
// => 'Wrong!'
executes the function

immediately The combination of operator and function can make the function execute immediately. In fact, there are many operators that make functions execute immediately, such as +, -, !, ~, and the void operators mentioned in this article. Also, there are the most common parentheses (not operators, which change precedence):

(function(){
    console.log('execute now');
})();
// => 'execute now'
acts as javascript : The URI of the protocol is

used for the like, favorite and other buttons in the web page. If the a tag is used to implement it, similar codes such as href="javascript:void(0)" are often used. The function of this code is when the link is clicked. Let the page not jump.

In href="javascript:void(0)", a URI starting with the javascript: protocol is used, the browser will evaluate the code after the colon by default, and then display the result in a new page, with one exception , if the result is undefined, the browser will not refresh the page to render the new value.

In fact, we have many ways to achieve the effect of likes and collections without using such a hack - although it is really convenient.

The javascript: protocol declares that the body of the URL is arbitrary javascript code, which is compiled and executed by the javascript interpreter. Usually, javascript: URL is used to execute some code that does not change the current page document. To do this, you must ensure that the last statement of the URL There is no return value, such as using void 0.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327067774&siteId=291194637