JS Definitive Guide Reading Notes

Lexical structure

  • unicode is prefixed with \u followed by 4 hex digits
  • Identifiers must start with a letter, underscore, or $, and subsequent characters can be letters, numbers, underscore, or $.

Types, Values ​​and Variables

  • js data types are divided into two categories: primitive types and object types
  • Primitive types include: Number, String and Boolean
  • Two special primitive values: null and undefined
  • The js interpreter has its own memory management mechanism, which can automatically perform garbage collection (Garbage Collection) on memory.
  • js integer range is -2^53 ~ 2^53
  • Infinity can be represented by Infinity
  • Infinity = Number.POSITIVE_INFINITY = 1/0 = Number.MAX_VALUE + 1
  • Use NaN for non-numeric values
  • NaN = Number.NaN = 0/0 = Number.MIN_VALUE / 2
  • -NaN is not equal to any value, including itself. To use x!=x to determine if x is NaN or use isNaN()
  • Binary floating point notation cannot exactly represent numbers like 0.1:
var x = .3 - .2;
var y = .2 - .1;
x == y; // false
  • String is an immutable ordered sequence of 16-bit values
  • undefined,null,0,-0,NaN,"" will be converted to false, other values ​​including objects (arrays) will be converted to true
  • Null performs the typeof operation, and the result returns "object"
  • Two separate objects are never equal, and two separate arrays are never equal:
var o = {x:1},p = {x:1};
o == p; // => false
o === p; // => false
var a = [], b = [];
a == b; // => false
a === b; // => false
  • Javascript type conversion
value convert to - - -
- string number Boolean value object
undefined "undefined" NaN false throws TypeError
null "null" 0 false throws TypeError
true "true" 1 - new Boolean(true)
false "false" 0 - new Boolean(false)
"" - 0 false new String("")
"1.2" - 1.2 true new String("1.2")
"sevens" - NanN true new String("sevens")
0 "0" - false new Number(0)
-0 "0" - false new Number(-0)
NaN "NaN" - false new Number(NaN)
1 "1" - true new Number(1)
{} Refer to Section 3.8.3 Refer to Section 3.8.3 true  
[] "" 0 true  
[9] "9" 9 true  
['a'] Use the join method NaN true  
function(){} Refer to Section 3.8.3 NaN true  
  • About == equality judgment
null == undefined; // true
"0" == 0; //比较前字符串转换成数字 true
0 == false; //布尔转换成数字 true
"0" == false; //字符串和布尔值都转换成数字 true

- 对日期对象进行转换
````javascript
var now = new Date();
typeof (now + 1); // => string , "+"将日期转换成了字符串
typeof (now - 1); // => Number: "-" 将对象转换成了数字

statement in advance

var scope = "global";
function f(){
    console.log(scope); // undefined
    var scope = "local";
    console.log(scope); // local
}

Since local variables are always defined in the entire function body, that is to say, the local variables in the function body shadow the global variables of the same name. but! It is only assigned when var is executed. So the above code is equivalent to:

var scope = "global";
function f(){
    var scope;
    console.log(scope); // undefined
    scope = "local";
    console.log(scope); // local
}
  • scope chain *

Expressions and Operators

  • 2 + null => 2
  • 2 + undefined => NaN
  • "==" will convert the values ​​on the left and right sides if there is no string, and then convert the numeric type before comparing, see the conversion diagram above.

statement

  • If the var statement appears in the body of a function, it defines a local variable and the scope is the function. If you use a var statement in top-level code, it declares a global variable that is visible throughout the javascript program. Global variables declared by var cannot be deleted by delete.
  • Because the value of the variable in the for/in loop can be used as the lvalue of the assignment expression, you can use for..in.. to quickly copy the property key of an object into an array:
var o = {x:1,y:2,z:3};
var a = [],i = 0;
for(a[i++] in o) ;
  • "use strict" applies to using the specified scope

object

  • Simulate inheritance, create a new object through prototypal inheritance
function inherit(p) {
    if (p == null) throw TypeError();
    //如果Object.create()存在,则直接使用它
    if (Object.create)
        return Object.create(p)
    //否则进一步检测
    var t = typeof p;
    if (t !== "object" && t !== 'function')
        throw TypeError();
    //定义一个空构造函数
    function f() {}; 
    //将其原型属性设置为p
    f.prototype = p;
    return new f();
} 
  • One of the uses of the inherit() function is to prevent library functions from inadvertently modifying objects that are not under our control. Instead of passing the object directly into the function as a parameter, pass its inherited object into the function.
  • var len = book && book.subtitle && book.subtitle.length
  • Use in, hasOwnProperty() to determine whether the target object has a target property
  • The properties of the descriptor object of the data property are value, writable, enumerable and configurable.
  • Inherited function to copy property
Object.defineProperty(Object.prototype,"extend",
    {
        writable: true,
        enumerable: false,
        configurable: true,
        value: function (o){
            //得到所有自有属性,包括不可枚举属性
            var names = Object.getOwnPropertyNames(o);
            //遍历
            for (var i = 0; i < names.length; i++){
                if (names[i] in this) continue;
                var desc = Object.getOwnPropertyDescriptor(o,names[i]);
                Object.defineProperty(this,names[i],desc);
            }
        }
    }
);

To be Continue.. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325452999&siteId=291194637
Recommended