zoukankan      html  css  js  c++  java
  • JavaScript Patterns 3.7 Primitive Wrappers

    Primitive value types: number, string, boolean, null, and undefined.

    // a primitive number
    
    var n = 100;
    
    console.log(typeof n); // "number" 
    
    
    // a Number object
    
    var nobj = new Number(100);
    
    console.log(typeof nobj); // "object" 

    One reason to use the wrapper objects is when you want to augment the value and persist state.  Because primitives are not objects, they  cannot be augmented with properties. 

    // primitive string
    
    var greet = "Hello there";
     
    
    // primitive is converted to an object
    
    // in order to use the split() method
    
    greet.split(' ')[0]; // "Hello"
     
    
    // attemting to augment a primitive is not an error
    
    greet.smile = true;
     
    
    // but it doesn't actually work
    
    typeof greet.smile; // "undefined" 
    When used without new, wrapper constructors convert the argument passed to them to a primitive value:
    typeof Number(1); // "number"
    
    typeof Number("1"); // "number"
    
    typeof Number(new Number()); // "number"
    
    typeof String(1); // "string"
    
    typeof Boolean(1); // "boolean"
    
    
  • 相关阅读:
    树莓派开机启动
    树莓派连接18b20测温度
    树莓派VNC
    树莓派python 控制GPIO
    树莓派笔记
    用nohup执行python程序时,print无法输出
    mysql 函数应用
    mysql 正则表达式判断是否数字
    mysql select into 不支持
    tushare
  • 原文地址:https://www.cnblogs.com/haokaibo/p/primitive-wrappers.html
Copyright © 2011-2022 走看看