美文网首页
类型和语法--原生原型

类型和语法--原生原型

作者: 怪兽别跑biubiubi | 来源:发表于2019-07-10 15:18 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    // var a = new String('abc');
    // console.log(a);  // String {0: "a", 1: "b", 2: "c", length: 3, [[PrimitiveValue]]: "abc"}
    // console.log(typeof a); // object
    // console.log(a instanceof String);  // true

    // ----------------------------------------------------------------------------------------------
      // new String("abc") 创建的是字符串 "abc" 的封装对象,而非基本类型值 "abc"。

      // 在 JavaScript 中,判断一个变量的类型尝尝会用 typeof 运算符,
      // 在使用 typeof 运算符时采用引用类型存储值会出现一个问题,
      // 无论引用的是什么类型的对象,它都返回 "object"。
      // ECMAScript 引入了另一个 Java 运算符 instanceof 来解决这个问题。
      // instanceof 运算符与 typeof 运算符相似,用于识别正在处理的对象的类型。与 typeof 方法不同的是,
      // instanceof 方法要求开发者明确地确认对象为某特定类型。例如:
    // ----------------------------------------------------------------------------------------------

    // console.log(Object.prototype.toString.call( [1,2,3] )); // "[object Array]" 
    // console.log(Object.prototype.toString.call( /regex-literal/i )); // "[object RegExp]"
    // Object.prototype.toString.call( "abc" ); // "[object String]" 
    // Object.prototype.toString.call( 42 ); // "[object Number]" 
    // Object.prototype.toString.call( true ); // "[object Boolean]"

    // ----------------------------------------------------------------------------------------------
      // Object.prototype.toString:查看内部属性 [[Class]]
    // ----------------------------------------------------------------------------------------------

    // var a = new String( "abc" ); 
    // var b = new Number( 42 ); 
    // var c = new Boolean( true ); 
    // console.log(a.valueOf()); // "abc" 
    // console.log(b.valueOf()); // 42 
    // console.log(c.valueOf()); // true

    // ----------------------------------------------------------------------------------------------
      // valueOf:得到封装对象中的基本类型值
    // ----------------------------------------------------------------------------------------------
    // ************* 在需要用到封装对象中的基本类型值的地方会发生隐式拆封。 *************

    // ========================================================================================================
    // Array(..)
      // var a = new Array(1, 2, 3);
      // console.log(a);  // [1, 2, 3]

      // var b = new Array(1);
      // console.log(b);  // length: 1;length被设置成了指定的值为1

      // var a = new Array(3);
      // var b = [undefined, undefined, undefined];
      // var c = [];
      // c.length = 3;
      // console.log(a);  // [undefined × 3] length: 3
      // console.log(b);  // [undefined, undefined, undefined]
      // console.log(c);  // [undefined × 3] length: 3

      // var a = Array.apply( null, { length: 3 } ); 
      // console.log(a); // [ undefined, undefined, undefined ]

    // ----------------------------------------------------------------------------------------------
      // Array 构造函数只带一个数字参数的时候,该参数会被作为数组的预设长度(length),而 非只充当数组中的一个元素。
      // 我们将包含至少一个“空单元”的数组称为“稀疏数组”。
      // ,join(..) 首先假定数组不为空,然后通过 length 属性值来遍历其中的元 素。而 map(..) 并不做这样的假定
    // ----------------------------------------------------------------------------------------------

    // ========================================================================================================
      // var c = new Object(); 
      // c.foo = "bar"; 
      // console.log(c); // { foo: "bar" } 

      // var d = { foo: "bar" }; 
      // console.log(d); // { foo: "bar" } 

      // var e = new Function( "a", "return a * 2;" ); 
      // console.log(e);  // function anonymous(a) {return a * 2;}

      // var f = function(a) { return a * 2; } 
      // console.log(f);  // function (a) { return a * 2; }

      // function g(a) { return a * 2; } 
      // console.log(g);  // function g(a) { return a * 2; }

      // var h = new RegExp( "^a*b+", "g" ); 
      // console.log(h);  // /^a*b+/g

      // var i = /^a*b+/g;
      // console.log(i);  // /^a*b+/g

    // ----------------------------------------------------------------------------------------------
      // 没有必要使用 new Object() 来创建对象,因为这样就无法像常量形式那样一 次设定多个属性,而必须逐一设定。
    // ----------------------------------------------------------------------------------------------

    // ========================================================================================================
    // Date(..) 和 Error(..) 
      // if (!Date.now) {
      //   Date.now = function(){      
      //     console.log((new Date()).getTime());   
      //     return (new Date()).getTime();
      //   }; 
      // }

      // var myDate = new Date(); // Wed Jul 10 2019 14:23:04 GMT+0800 (中国标准时间)
      // myDate.getYear(); //获取当前年份(2位)
      // myDate.getFullYear(); //获取完整的年份(4位,1970-????)
      // myDate.getMonth(); //获取当前月份(0-11,0代表1月)
      // myDate.getDate(); //获取当前日(1-31)
      // myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
      // myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
      // myDate.getHours(); //获取当前小时数(0-23)
      // myDate.getMinutes(); //获取当前分钟数(0-59)
      // myDate.getSeconds(); //获取当前秒数(0-59)
      // myDate.getMilliseconds(); //获取当前毫秒数(0-999)
      // myDate.toLocaleDateString(); //获取当前日期
      // var mytime=myDate.toLocaleTimeString(); //获取当前时间
      // myDate.toLocaleString( ); //获取日期与时间


      // 对Date的扩展,将 Date 转化为指定格式的String
      // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 
      // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
      // 例子: 
      // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
      // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 

      // Date.prototype.Format = function (fmt) {
      //     var o = {
      //         "M+": this.getMonth() + 1, //月份 
      //         "d+": this.getDate(), //日 
      //         "H+": this.getHours(), //小时 
      //         "m+": this.getMinutes(), //分 
      //         "s+": this.getSeconds(), //秒 
      //         "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
      //         "S": this.getMilliseconds() //毫秒 
      //     };
      //     if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
      //     for (var k in o)
      //     if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      //     return fmt;
      // }

      // // 调用: 
      // var time1 = new Date().Format("yyyy-MM-dd");
      // var time2 = new Date().Format("yyyy-MM-dd HH:mm:ss");
      // console.log(time1);  // 2019-07-10
      // console.log(time2);  // 2019-07-10 14:44:04

    // ----------------------------------------------------------------------------------------------
      // 创建日期对象必须使用 new Date()。Date(..) 可以带参数,用来指定日期和时间,而不带 参数的话则使用当前的日期和时间。
    // ----------------------------------------------------------------------------------------------

    // ========================================================================================================
    // Symbol
    // var mysym = Symbol( "my own symbol" ); 
    // console.log(mysym);              // Symbol(my own symbol) 
    // console.log(mysym.toString());   // "Symbol(my own symbol)" 
    // console.log(typeof mysym);       // "symbol" 

    // var a = { }; 
    // a[mysym] = "foobar"; 
    // console.log(Object.getOwnPropertySymbols( a )); // [ Symbol(my own symbol) ]

    // ----------------------------------------------------------------------------------------------
      // ES6 中新加入了一个基本数据类型 ——符号(Symbol)。符号是具有唯一性的特殊值(并 非绝对),用它来命名对象属性不容易导致重名。
      // 符号并非对象,而是一种简单标量基本类型。
    // ----------------------------------------------------------------------------------------------
      

    // ========================================================================================================
    // 原生原型
    var a = ' abcdefg ';
    console.log(a.indexOf('e'));  // 5
    console.log(a.charAt(3));  // c
    console.log(a.substr(1, 4));  // abcd
    console.log(a.toUpperCase());  // " ABCDEFG "
    console.log(a.trim());  // "abcdefg"

    // ----------------------------------------------------------------------------------------------
      // String# === String.prototype;
      // • String#indexOf(..) 在字符串中找到指定子字符串的位置。
      // • String#charAt(..) 获得字符串指定位置上的字符。
      // • String#substr(..)、String#substring(..) 和 String#slice(..) 获得字符串的指定部分。  
      // • String#toUpperCase() 和 String#toLowerCase() 将字符串转换为大写或小写。
      // • String#trim() 去掉字符串前后的空格,返回新的字符串。
      // 以上方法并不改变原字符串的值,而是返回一个新字符串。

      // Number#tofixed(..)(将 数字转换为指定长度的整数字符串)和 Array#concat(..)(合并数组)
    // ----------------------------------------------------------------------------------------------


    // 小结
    // JavaScript 为基本数据类型值提供了封装对象,称为原生函数(如 String、Number、Boolean 等)。
    // 它们为基本数据类型值提供了该子类型所特有的方法和属性(如:String#trim() 和 Array#concat(..))。
    // 对于简单标量基本类型值,比如 "abcdefg",如果要访问它的 length 属性或 String.prototype 方法,
    // JavaScript 引擎会自动对该值进行封装(即用相应类型的封装对象来包装它)来实现对这些属性和方法的访问。
  </script>
</body>
</html>

相关文章

网友评论

      本文标题:类型和语法--原生原型

      本文链接:https://www.haomeiwen.com/subject/ckehkctx.html