You do not know those little knowledge of javascript

This article will introduce some of the javascriptpopular knowledge of, after reading that pose a rose like oh do not forget one point.

Immediately execute the function
javascriptThe function has executed immediately, immediately execute the function is there are a lot of benefits. If you think I'm just introduce you to two well-known writing function is executed immediately, you still law-abiding, read on.

First of all, we all know that immediately execute the function, there are two ways to write:

// 第一种
(function() {
	alert("hello world");
})()
// 第二种
(function() {
    alert("hello world");
}())

I'm sure a lot of people do not understand why this is so should write. (If you know, when I did)
principle: Explain in a first form of writing, the first ()wrapped function itself, so that the body of the function into aexpression. Simply put, the ()wrapping portions are referred to as an expression; the second ()is a function of performing symbol, is performed in front of a particular expression.
If you read the above statements still do not understand, or skeptical, we can be verified by the following:

+ function() {
	alert("hello world");
}()

Yes, you read that right, we will first ()rewrite into a +, we know +the content of the action will be a special expression, when we run the code, and there is no error, on the contrary, the browser will pop up a warning frame. In fact, we will +replace the -other operators are possible.

One thing to note is that when we simply will +be replaced -when the function is executed, but replaced *, /or %will be error when these operations, this time we just need to give *, /or %preceded by any of a number you can perform normal a. The reason is simple, because the individual +and -also havePositive and negativeThe operation, so use *, /or %when you need to make it rightexpression. E.g:
javascript 1 * function() { alert("hello world"); }()

I believe you have read the above explanationImmediately execute the functionWe have a more profound understanding.

typeof

typeof is a function that returns the type of data. We all know that would have been an error in the use of undeclared variables, such as:

var a = 10;
console.log(b); // b is not defined

But when we typeof()use an undeclared variable will not be in error, and returns undefined.

var a = 10;
console.log(typeof(b)); // undefined

undefinedversusnull

First we look at this example:

console.log(a); // undefined
var a = 10;
console.log(a); // 10

We all know that this is a pre-compiled process, it is popularVariable declaration upgrade, So the first results should be printed undefined. But you have not tried this:

console.log(a == null); // true
var a = 10;

Yes, which explains undefined == null, this knowledge we can also find from one place:

console.log(undefined > 0); // false
console.log(undefined < 0); // false
console.log(undefined == 0); // false
console.log(null > 0); // false
console.log(null > 0); // false
console.log(null > 0); // false

We found undefinedand nullareno greater thannot less thanAndnot equal to0。

Then we can venture a guess that they are equal:

console.log(undefined == null); // true
console.log(undefined === null); // false

Note that undefined !== null, because undefinedis not defined, but nullit means an empty object.

NaN

We know that any data related to equal itself, but here's a special case is NaNthe need to remember is NaNnot equal to any of the data,Including itself(Who do not recognize, including their own):

console.log(NaN == NaN); // false
console.log(NaN == NaN); // false
Published 18 original articles · won praise 1 · views 317

Guess you like

Origin blog.csdn.net/october_autumn/article/details/104720558