JS interesting knowledge

1. The semicolon and newline

function fn1(){
    return {
        name: 'javascript'
    };
}

function fn2(){
    return 
    {
        name: 'javascript'
    };
}

var obj1 = fn1();
var obj2 = fn2();

console.log(obj1);
console.log(obj2);

  This example looks no different, but in essence, there is a difference. The output is a first object, while the second output is undefined. why?

Js is not required because the semicolon and wrap this thing, and the second will be resolved to:

function fn2(){
    return ;
    {
        name: 'javascript'
    };
}

  So essentially a return is undefined.

 

2. Type in the top five, with regard null

There are five types of data Js boolean, string, number, undefined, object. And why

typeof null

It will be resolved to object?

The reason is that in binary format is stored in the data storage underlying computer, if the first three to 0, then the data are parsed into object types. All digits are null and zero, when resolving null, the top three to be resolved, the computer was mistaken null object type, which belongs to a bug computer language level.

 

3. About undefined

Examples of Undefined data type is undefined, although it can not be used to store data in the actual application process, but we can do something for him.

When we detect the type Undefined data type names

    var Undefined = 10;
    var String=10;
    console.log(typeof Undefined);
    console.log(typeof String);

Showing results: number number. This is not because Undefined keywords and reserved words. If the keyword is declared error.

 

When we detect a data type for him

console.log(typeof undefined)

Showing results is undefined.

 

When we detect other types of data types

console.log(typeof Object);
console.log(typeof Number);
console.log(typeof String);
console.log(typeof Boolean);

Display the results data type is not the type itself, but the function;

However, due to undefined not keywords and reserved words, so we can re-declare it

    var undefined = 10;
    var string=10;
    console.log(typeof undefined);
    console.log(typeof string);

Display results as: number number; Visible property has undefined constant values. At the same time the beginning of the lowercase letter

   var undefined = 10;
    var string=10;
    var boolean=10;
    var object=10;
    var number=10;

    console.log(typeof undefined);
    console.log(typeof string);
    console.log(typeof object);

    console.log(typeof boolean);
    console.log(typeof number);

Showing results: undefined number number number number. Visible with the same name and the first letter lowercase a data type instance only undefined, the other not.

 

4. The absence of overloaded functions in JavaScript

    Students must have knowledge of the backend knows there is function overloading in C ++ and Java,

  But does not exist in JavaScript function overloading, if there is, who will cover the former. And function declarations will increase.

 

Guess you like

Origin www.cnblogs.com/bigharbour/p/11423268.html