JS often ignores rules

1. The special Number NaN is not equal to all other values, including itself

NaN === NaN; // false

The only way to judge NaN is through the isNaN() function

isNaN(NaN); // true

2. Floating-point numbers will produce errors in the operation process, because the computer cannot accurately represent the infinite loop decimal. To compare whether two floating-point numbers are equal, you can only calculate the absolute value of their difference to see if it is less than a certain threshold:

Math.abs(1 / 3 - (1 - 2 / 3)) < 0.0000001; // true

3. null和undefined

Attributes Explanation
null Represents an "empty" value. It is different from 0 and the empty string ``, 0 is a numeric value,'' represents a string with a length of 0, and null represents "empty".
undefined It means "undefined".

4. The string is immutable. If you assign a value to a certain index of the string, there will be no error, but it has no effect.

var s = 'Test';
s[0] = 'X';
alert(s); // s仍然为'Test'

5. Assigning a new value directly to the length of the Array will cause the size of the Array to change

var arr = [1, 2, 3];
arr.length; // 3
arr.length = 6;
arr; // arr变为[1, 2, 3, undefined, undefined, undefined]
arr.length = 2;
arr; // arr变为[1, 2]

6. If the index exceeds the range when assigning by index, it will also cause the Array size to change

var arr = [1, 2, 3];
arr[5] = 'x';
arr; // arr变为[1, 2, 3, undefined, undefined, 'x']

7. If in judges that an attribute of the object exists, this attribute may not necessarily belong to the object, it may be inherited

var xiaoming = {
    
    
    name: '小明'
};
'toString' in xiaoming; // true
//因为toString定义在object对象中,而所有对象最终都会在原型链上指向object,所以xiaoming也拥有toString属性。

8. To judge whether a property is owned by Xiaoming itself, rather than inherited, you can use the hasOwnProperty() method

var xiaoming = {
    
    
    name: '小明'
};
xiaoming.hasOwnProperty('name'); // true
xiaoming.hasOwnProperty('toString'); // false

9. In multiple if...else... statements, if a certain condition is established, the subsequent judgment will not continue.

var age = 20;
if (age >= 6) {
    
    
    console.log('teenager');
} else if (age >= 18) {
    
    
    console.log('adult');
} else {
    
    
    console.log('kid');
}
//输出:teenager

10. JavaScript treats null, undefined, 0, NaN, and empty strings as false, and all other values ​​are regarded as true

11. for… in loop
to loop out all the attributes of an object in turn

var o = {
    
    
    name: 'Jack',
    age: 20,
    city: 'Beijing'
};
for (var key in o) {
    
    
    console.log(key); // 'name', 'age', 'city'
}

Loop out the index of the Array (note: for… in looping on Array gets String instead of Number)

var a = ['A', 'B', 'C'];
for (var i in a) {
    
    
    console.log(i); // '0', '1', '2'
    console.log(a[i]); // 'A', 'B', 'C'
}

12. The JavaScript engine automatically promotes the declaration of variable y, but does not promote the assignment of variable y.

'use strict';

function foo() {
    
    
    var x = 'Hello, ' + y;
    console.log(x);
    var y = 'Bob';
}

foo();

For the above foo() function, the code seen by the JavaScript engine is equivalent to:

function foo() {
    
    
    var y; // 提升变量y的申明,此时y为undefined
    var x = 'Hello, ' + y;
    console.log(x);
    y = 'Bob';
}

13. Namespace
Global variables will be bound to the window. If different JavaScript files use the same global variables or define top-level functions with the same name, they will cause naming conflicts and are difficult to find.

One way to reduce conflicts is to bind all of your variables and functions to a global variable.

E.g:

// 唯一的全局变量MYAPP:
var MYAPP = {
    
    };

// 其他变量:
MYAPP.name = 'myapp';
MYAPP.version = 1.0;

// 其他函数:
MYAPP.foo = function () {
    
    
    return 'foo';
};

Putting all your own code into the unique name space MYAPP will greatly reduce the possibility of global variable conflicts.

Many famous JavaScript libraries do this: jQuery, YUI, underscore, etc.

Guess you like

Origin blog.csdn.net/qq_17627195/article/details/109110972