"You do not know Javascript-- in volume study concluded" (type, value)

Types of

Is a type of internal features, which are defined 值的行为so as to distinguish it from other values.

Built-in type

1 or seven built-in types:

  • Null (null)
  • Undefined (undefined)
  • Boolean values ​​(boolean)
  • Digital (number)
  • String (string)
  • Object (object)
  • Symbol (symbol, ES6 are new)

2, we can use the typeofoperator to view the type of the value, it returns the type 字符串值.

3, since the typeof null === 'object', so that a null value detection type requires the following conditions this complex

    var a = null;
    (!a && typeof a == "object"); // true
复制代码

4, is actually a function of the object "子类型". Specifically, the function is "callable object", which has an internal property [[call]], the property so that it can be called.

5, the object function length属性is其声明的参数的个数

Value and type

1, Javascript is 变量是没有类型的only only. Variables can hold any type of value at any time.

2, has been in scope 声明but also 没有赋值the variable is undefined.

3, also 没有在作用域中声明过得变量it is undeclared.

    var a;
    typeof a // "undefined"
    typeof b // "undefined"   没有声明还是undefined 这主要是因为typeof的安全防范机制
复制代码

typeof security mechanism

  • Inspection of global variables
    // 这样是安全的
    if(typeof DEBUG ! == "undefined"){
        console.log('xxxxx')
    }
复制代码
  • Whether non-global variable definition
    (function(){
        function FeatureXYZ(){};
        
        function doSomethingCool(){
            var helper = (typeof FeatureXYZ !== "undefined") ? FeatureXYZ :
            function(){
                // default feature
            }
            
            var a = helper();
        }
        doSomethingCool()
    })()
复制代码
  • Parameter detection function is passed
    function doSomethingCool(FeatureXYZ){
        var helper = FeatureXYZ || function(){}
        var val = helper();
        // ...
    }
复制代码

value

Array

1, the array can hold any type of value, which can be 字符串, , 数字, 对象or even 其他数组.

2, use the delete operator unit may be removed from the array, but please note that the unit is removed, the array length property 并不会发生变化.

3, creating "sparse" array (i.e. an array containing empty or vacant units). a [1] the value is undefined, but the display will be assigned undefined (a [1] = undefined) is differentiated.

    var a = [];
    a[0] = 1;
    a[2] = [3];
    a[1] // undefined
    
    a.length // 3
复制代码

4, through an array 数字进行索引, but they are objects, so it can contain strings 键值and 属性(but these 不计算在数组长度内).

5, if 字符串键值能够被强制类型转换为十进制数字的话it will be handled as a numeric index

    var a = [];
    a[0] = 1;
    a['foo'] = 2;
    a.length // 1
    a['foobar'] //2
    a.foobar //2
    
    
    
    var a = [];
    a['13'] = 42;
    a.length // 14
复制代码

6, the array type => array of ways

  • slice
    function foo(){
        var arr = Array.prototype.slice.call(arguments);
        arr.push('bam');
        console.log(arr)
    }
    
    foo('bar','baz') // ['bar','baz','bam']
复制代码
  • Array.from() (ES6)
    var arr = Array.from(arguments)
复制代码

String

1, Javascript is immutable string (string member function means 不会改变其原始值, but to create and return a 新的字符串), and the array is variable.

2, no reverse string method, alternative methods may then be converted into a string array.

    var a = 'foo'
    var c = a.split("").reverse().join("")
    c; //"oof"
复制代码

digital

1, Javascript type is based on the numbers IEEE754标准achieved, this standard is also often referred to as "float." Javascript using the "double" format.

2, toFixed () method can be specified 小数部分的显示位数, if the specified number of display digits of the fractional part of the actual number of bits than would padded with zeros.

3, toPrecision () method is used to specify the 有效数位displayed digits.

4 ,. given operator requires special attention because it is a valid 数字字符and will be identified as a priority 数字字面量的一部分, and only then 对象属性访问运算符.

    // 无效语法
    42.toFixed(3)  // SyntaxError
    
    // 下面的语法都有效
    (42).toFixed(3) // 42.000
    0.42.toFixed(3) // 0.420
    42..toFixed(3) // 42.000
复制代码

5, binary floating biggest problem (follow all IEEE754 specification language is so) decimal precision is not allowed. (E.g., equal to 0.1 + 0.2 0.3 === classic problem of false)

    // 判断两个小数相等
    function numberCloseEnoughToEqual(n1,n2){
        return Math.abs(n1-n2) < Number.EPSILON;
    }
    var a = 0.1 + 0.2;
    var b = 0.3;
    numberCloseEnoughToEqual(a,b) // true
复制代码

6, may be used Number.isInteger () to detect whether a value is an integer.

    Number.isInteger(42) // true
    Number.isInteger(42.000) // true
    Number.isInteger(42.3) // false
    
    
    // polyfill
    if(!Number.isInteger){
        Number.isInteger = function(num){
            return typeof num === 'number' && num % 1 == 0
        }
    }
复制代码

7, a | 0 can be converted value the variable a is a signed integer 32, because the number of bitwise | applies only to 32-bit integers (only concerned with the value of 32 within the other digits will be ignored).

8, undefined type has only one value, i.e. undefined. only one type of null value, i.e. null. Their value is the name of both types. undefined指从未赋值,null只曾赋过值,但是目前没有值

9, null is a special 关键字, not 标识符, we can not be used as a variable and assignment. However, it is undefined 一个标识符and can be used as a variable and assignment.

10, in a non-strict mode, we can assign a global identifier undefined

    function foo(){
        undefined = 2; // 非常糟糕的做法
    }
    
    foo();
    
    function foo(){
        "use strict";
        undefined = 2; // TypeError
    }
    
    foo()
复制代码

11, we can void运算符can get undefined. Expressions void __ no return value, so the result is undefined. void and 不改变表达式的结果just 让表达式不返回值. Usually we use to get the void 0 undefined.

    var a = 42;
    console.log(void a,a) // undefined 42
复制代码

12, not a number Number (NaN)

  • NaN is a 数字type of
    var a = 2 / "foo"; // NaN
    typeof a === 'number' // true
复制代码
  • Detection NaN
    // 全局函数isNaN判断。(但是有一个缺陷,就是当传递一个非数字的时候,isNaN也返回true)
    var a = 2 / 'foo';
    var b = 'foo';
    
    window.isNaN(a) // true 
    window.isNaN(b) // true 
    
    // ES6 的Number.isNaN
    if(!Number.isNaN){
        Number.isNaN = function(n){
            return (
                typeof n === "number" && 
                window.isNaN(n)
            )
        }
    }
    
    // 最简单的方法,NaN是Javascript中唯一一个不等于自身的值
    if(!Number.isNaN){
        Number.isNaN = function(){
            return n !== n;
        }
    }
复制代码

13, a value of zero

  • Javascript has a regular and a -0 0
  • To 负零perform 字符串化and returns a '0'
    var a = 0 / -3;
    a; // -0
    a.toString() // '0'
    a+'' // '0'
    String(a) // '0'
复制代码
  • It will be 字符串converted 数字, and the result is accurate.
   +"-0" // -0
   Number("-0") // -0
   JSON.parse("-0") // -0
复制代码
  • Judgment -0
   function isNegZero(n){
       n = Number(n);
       return (n===0) && (1/n===-Infinity)
   }
复制代码

14, ES6 new method of adding a tool Object.is()to determine whether the two absolute values are equal (and added NaN judgment -0)

   if(!Object.is){
       Object.is = function(v1,v2) {
           // 判断是否为-0
           if(v1===0 && v2===0){
               return 1/v1 === 1/v2
           }
           //判断是否为NaN
           if(v1!==v1){
               return v2!==v2;
           }
           
           // 其他情况
           return v1 === v2;
       }
   }
复制代码

Guess you like

Origin blog.csdn.net/weixin_33778544/article/details/91364892