zoukankan      html  css  js  c++  java
  • es6笔记1^_^let、string、number、math

      ECMAScript是一种由Ecma国际(前身为欧洲计算机制造商协会,英文名称是European Computer Manufacturers Association)通过ECMA-262标准化的脚本程序设计语言。这种语言在万维网上应用广泛,它往往被称为JavaScriptJScript,但实际上后两者是ECMA-262标准的实现和扩展。ECMAScript 6.0(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了。它的目标,是使得JavaScript语言可以用来编写复杂的大型应用程序,成为企业级开发语言。

      嫌babel麻烦,直接使用Traceur转码器-Google公司的Traceur转码器,可以将ES6代码转为ES5代码。这意味着,你可以用ES6的方式编写程序,又不用担心浏览器是否支持。Traceur允许将ES6代码直接插入网页。首先,必须在网页头部加载Traceur库文件。

      <!-- 加载Traceur编译器 -->
        <script src="http://google.github.io/traceur-compiler/bin/traceur.js" type="text/javascript"></script>
        <script src="https://google.github.io/traceur-compiler/bin/BrowserSystem.js"></script>
        <!-- 将Traceur编译器用于网页 -->
        <script src="http://google.github.io/traceur-compiler/src/bootstrap.js" type="text/javascript"></script>
    <script type="module">
        class Calc {
            constructor(){
                console.log('Calc constructor');
            }
            add(a, b){
                return a + b;
            }
        }
        var c = new Calc();
        console.log(c.add(4,5));//9
    </script>

      这样就可以欣赏es6咯!我在写这篇博文的时候把chrome升级到了最新版,大部分es6都支持了。如若报错,还是老实按上面的方法加编译器吧。

    一、let

      let是ES6中新增关键字。它的作用类似于var,用来声明变量。const声明一个只读的常量。一旦声明,常量的值就不能改变。
      ES5只有全局作用域和函数作用域,没有块级作用域,这带来很多不合理的场景。
      而let则实际上为JavaScript新增了块级作用域。用它所声明的变量,只在let命令所在的代码块内有效。
        if(1){
            var a=1;
            let b=2;
            console.log(a);
            console.log(b);
        }
        console.log(a);
    //    console.log(b);//报错 b is not defined(…)
    
        if(1){//只在此作用域有效
           const c=3;
            console.log(c);
        }
    //    console.log(c);//报错 b is not defined(…)
      一个var带来的不合理场景就是用来计数的循环变量泄露为全局变量,看下面的例子:
    var a = [];
    for (var i = 0; i < 10; i++) {
        a[i] = function () {
            console.log(i);
        };
    }
    a[6](); // 10
    //上面代码中,变量i是var声明的,在全局范围内都有效。所以每一次循环,新的i值都会覆盖旧值,导致最后输出的是最后一轮的i的值。而使用let则不会出现这个问题。
    var aa = [];
    for (let ii = 0; ii < 10; ii++) {
        aa[ii] = function () {
            console.log(ii);
        };
    }
    aa[6](); // 6

    二、repeat()

      str.repeat()返回一个新字符串,表示将原字符串重复n次。

        if(1){
            let str='nick';
            let str1=str.repeat(2);
            console.log(str);
            console.log(str1);
        }

    三、拼接字符串

      模板字符中,支持字符串插值,模板字符串可以包含空格多行。
      用`来标识起始,用${}来引用变量,而且所有的空格和缩进都会被保留在输出之中,``这2个点不是引号,是esc下面那个~、(英文状态)。
        if(1){
            let first='web';
            let last='nick';
            console.log(`Hello ${first} ${last}!`);
            console.log(`Hello ${first}    ${last}!`);
            console.log(`Hello ${first}
            ${last}!`);
        }

      以上常用,下面的作为了解

    四、String.raw()

    若使用String.raw 作为模板字符串的前缀,则模板字符串可以是原始(raw)的。反斜线也不再是特殊字符,
     也不会被解释成换行符:
        if(1){
            let raw=String.raw`hi : 
    `;
            console.log(raw==='hi : \n');
        }

    五、Number.isFinite()、Number.isNaN()、Number.isInteger()

      Number.isFinite()用来检查一个数值是否非无穷(infinity)。Number.isNaN()用来检查一个值是否为NaN。
        Number.isFinite(15); // true
        Number.isFinite(0.8); // true
        Number.isFinite(NaN); // false
        Number.isFinite(Infinity); // false
        Number.isFinite(-Infinity); // false
        Number.isFinite("foo"); // false
        Number.isFinite("15"); // false
        Number.isFinite(true); // false
        Number.isNaN(NaN); // true
        Number.isNaN(15); // false
        Number.isNaN("15"); // false
        Number.isNaN(true); // false
    Number.isInteger()用来判断一个值是否为整数。需要注意的是,在JavaScript内部,整数和浮点数是同样的储存方法,所以3和3.0被视为同一个值。
    Number.isInteger(25) // true
    Number.isInteger(25.0) // true
    Number.isInteger(25.1) // false
    Number.isInteger("15") // false
    Number.isInteger(true) // false

    六、Math

      Math对象新增的方法,都是静态方法,只能在Math对象上调用。
      Math.trunc():去除一个数的小数部分,返回整数部分。
    Math.trunc(4.1) // 4
    Math.trunc(-4.1) // -4
    注意:对于空值和无法截取整数的值,返回NaN。
    Math.sign():判断一个数到底是正数、负数、还是零。
    返回五种值:参数为正数,返回+1;参数为负数,返回-1;参数为0,返回0;参数为-0,返回-0;其他值,返回NaN。
    Math.sign(-5) // -1
    Math.sign(5) // +1
    Math.sign(0) // +0
    Math.sign(-0) // -0
    Math.sign('hubwiz'); // NaN
    //Math.cbrt:计算一个数的立方根。
    Math.cbrt(-1); // -1
    Math.cbrt(0);  // 0
    Math.cbrt(2);  // 1.2599210498948732
    //Math.fround:返回一个数的单精度浮点数形式。
    Math.fround(0);     // 0
    Math.fround(1.337); // 1.3370000123977661
    Math.fround(NaN);   // NaN
    //Math.hypot:返回所有参数的平方和的平方根。
    Math.hypot(3, 4);        // 5
    Math.hypot(3, 4, 5);     // 7.0710678118654755
    Math.hypot();            // 0
    Math.hypot(NaN);         // NaN
    Math.hypot(3, 4, 'foo'); // NaN
    Math.hypot(3, 4, '5');   // 7.0710678118654755
    Math.hypot(-3);          // 3
    //如果参数不是数值,Math.hypot方法会将其转为数值。只要有一个参数无法转为数值,就会返回NaN。
    /*8.Math 对数方法
    Math.expm1(x):返回ex - 1。*/
    Math.expm1(-1); // -0.6321205588285577
    Math.expm1(0);  // 0
    Math.expm1(1);  // 1.718281828459045
    //Math.log1p(x):返回1 + x的自然对数。如果x小于-1,返回NaN。
    Math.log1p(1);  // 0.6931471805599453
    Math.log1p(0);  // 0
    Math.log1p(-1); // -Infinity
    Math.log1p(-2); // NaN
    //Math.log10(x):返回以10为底的x的对数。如果x小于0,则返回NaN。
    Math.log10(2);      // 0.3010299956639812
    Math.log10(1);      // 0
    Math.log10(0);      // -Infinity
    Math.log10(-2);     // NaN
    Math.log10(100000); // 5
    //Math.log2(x):返回以2为底的x的对数。如果x小于0,则返回NaN。
    Math.log2(3);    // 1.584962500721156
    Math.log2(2);    // 1
    Math.log2(1);    // 0
    Math.log2(0);    // -Infinity
    Math.log2(-2);   // NaN
    Math.log2(1024); // 10
    //三角函数方法
    Math.sinh(3) //返回x的双曲正弦(hyperbolic sine)
    Math.cosh(3) //返回x的双曲余弦(hyperbolic cosine)
    Math.tanh(3)// 返回x的双曲正切(hyperbolic tangent)
    Math.asinh(3) //返回x的反双曲正弦(inverse hyperbolic sine)
    Math.acosh(3) //返回x的反双曲余弦(inverse hyperbolic cosine)
    Math.atanh(3) //返回x的反双曲正切(inverse hyperbolic tangent)

     此篇笔记整合:

    <!DOCTYPE html>
    <html >
    <head>
        <meta charset="UTF-8">
        <title>es6-string-number</title>
        <script>
    //1.let是ES6中新增关键字。它的作用类似于var,用来声明变量。const声明一个只读的常量。一旦声明,常量的值就不能改变。
    //ES5只有全局作用域和函数作用域,没有块级作用域,这带来很多不合理的场景。
    //而let则实际上为JavaScript新增了块级作用域。用它所声明的变量,只在let命令所在的代码块内有效。
        if(1){
            var a=1;
            let b=2;
            console.log(a);
            console.log(b);
        }
        console.log(a);
    //    console.log(b);//报错 b is not defined(…)
    
        if(1){//只在此作用域有效
           const c=3;
            console.log(c);
        }
    //    console.log(c);//报错 b is not defined(…)
    
    //一个var带来的不合理场景就是用来计数的循环变量泄露为全局变量,看下面的例子:
    var a = [];
    for (var i = 0; i < 10; i++) {
        a[i] = function () {
            console.log(i);
        };
    }
    a[6](); // 10
    
    //上面代码中,变量i是var声明的,在全局范围内都有效。所以每一次循环,新的i值都会覆盖旧值,导致最后输出的是最后一轮的i的值。而使用let则不会出现这个问题。
    
    var aa = [];
    for (let ii = 0; ii < 10; ii++) {
        aa[ii] = function () {
            console.log(ii);
        };
    }
    aa[6](); // 6
    
    
    //2.repeat()返回一个新字符串,表示将原字符串重复n次。
        if(1){
            let str='nick';
            let str1=str.repeat(2);
            console.log(str);
            console.log(str1);
        }
    //    3.模板字符中,支持字符串插值,模板字符串可以包含空格多行
    //用`来标识起始,用${}来引用变量,而且所有的空格和缩进都会被保留在输出之中,``这2个点不是引号,是esc下面那个~、(英文状态)
        if(1){
            let first='web';
            let last='nick';
            console.log(`Hello ${first} ${last}!`);
            console.log(`Hello ${first}    ${last}!`);
            console.log(`Hello ${first}
            ${last}!`);
        }
    /*4.String.raw()
    模板字符串可以是原始的
    ES6还为原生的String对象,提供了一个raw方法。
    若使用String.raw 作为模板字符串的前缀,则模板字符串可以是原始(raw)的。反斜线也不再是特殊字符,
     也不会被解释成换行符:*/
        if(1){
            let raw=String.raw`hi : 
    `;
            console.log(raw==='hi : \n');
        }
    
    
    //    5.Number.isFinite()用来检查一个数值是否非无穷(infinity)。Number.isNaN()用来检查一个值是否为NaN。
        Number.isFinite(15); // true
        Number.isFinite(0.8); // true
        Number.isFinite(NaN); // false
        Number.isFinite(Infinity); // false
        Number.isFinite(-Infinity); // false
        Number.isFinite("foo"); // false
        Number.isFinite("15"); // false
        Number.isFinite(true); // false
        Number.isNaN(NaN); // true
        Number.isNaN(15); // false
        Number.isNaN("15"); // false
        Number.isNaN(true); // false
    //    6.  Number.isInteger()用来判断一个值是否为整数。需要注意的是,在JavaScript内部,整数和浮点数是同样的储存方法,所以3和3.0被视为同一个值。
        Number.isInteger(25) // true
        Number.isInteger(25.0) // true
        Number.isInteger(25.1) // false
        Number.isInteger("15") // false
        Number.isInteger(true) // false
    //7.Math对象
    /*Math对象新增的方法,都是静态方法,只能在Math对象上调用。
    Math.trunc():去除一个数的小数部分,返回整数部分。*/
    Math.trunc(4.1) // 4
    Math.trunc(-4.1) // -4
    /*注意:对于空值和无法截取整数的值,返回NaN。
    Math.sign():判断一个数到底是正数、负数、还是零。
    返回五种值:参数为正数,返回+1;参数为负数,返回-1;参数为0,返回0;参数为-0,返回-0;其他值,返回NaN。*/
    Math.sign(-5) // -1
    Math.sign(5) // +1
    Math.sign(0) // +0
    Math.sign(-0) // -0
    Math.sign('hubwiz'); // NaN
    //Math.cbrt:计算一个数的立方根。
    Math.cbrt(-1); // -1
    Math.cbrt(0);  // 0
    Math.cbrt(2);  // 1.2599210498948732
    //Math.fround:返回一个数的单精度浮点数形式。
    Math.fround(0);     // 0
    Math.fround(1.337); // 1.3370000123977661
    Math.fround(NaN);   // NaN
    //Math.hypot:返回所有参数的平方和的平方根。
    Math.hypot(3, 4);        // 5
    Math.hypot(3, 4, 5);     // 7.0710678118654755
    Math.hypot();            // 0
    Math.hypot(NaN);         // NaN
    Math.hypot(3, 4, 'foo'); // NaN
    Math.hypot(3, 4, '5');   // 7.0710678118654755
    Math.hypot(-3);          // 3
    //如果参数不是数值,Math.hypot方法会将其转为数值。只要有一个参数无法转为数值,就会返回NaN。
    /*8.Math 对数方法
    Math.expm1(x):返回ex - 1。*/
    
    Math.expm1(-1); // -0.6321205588285577
    Math.expm1(0);  // 0
    Math.expm1(1);  // 1.718281828459045
    //Math.log1p(x):返回1 + x的自然对数。如果x小于-1,返回NaN。
    
    Math.log1p(1);  // 0.6931471805599453
    Math.log1p(0);  // 0
    Math.log1p(-1); // -Infinity
    Math.log1p(-2); // NaN
    //Math.log10(x):返回以10为底的x的对数。如果x小于0,则返回NaN。
    
    Math.log10(2);      // 0.3010299956639812
    Math.log10(1);      // 0
    Math.log10(0);      // -Infinity
    Math.log10(-2);     // NaN
    Math.log10(100000); // 5
    //Math.log2(x):返回以2为底的x的对数。如果x小于0,则返回NaN。
    
    Math.log2(3);    // 1.584962500721156
    Math.log2(2);    // 1
    Math.log2(1);    // 0
    Math.log2(0);    // -Infinity
    Math.log2(-2);   // NaN
    Math.log2(1024); // 10
    //三角函数方法
    Math.sinh(3) //返回x的双曲正弦(hyperbolic sine)
    Math.cosh(3) //返回x的双曲余弦(hyperbolic cosine)
    Math.tanh(3)// 返回x的双曲正切(hyperbolic tangent)
    Math.asinh(3) //返回x的反双曲正弦(inverse hyperbolic sine)
    Math.acosh(3) //返回x的反双曲余弦(inverse hyperbolic cosine)
    Math.atanh(3) //返回x的反双曲正切(inverse hyperbolic tangent)
    
    </script>
    </head>
    <body>
    
    </body>
    </html>
    View Code

     此篇终,待续……








  • 相关阅读:
    UVa 1354 天平难题 (枚举二叉树)
    广西邀请赛总结
    UVa 12118 检查员的难题 (dfs判连通, 构造欧拉通路)
    UVA
    Uva 127 "Accordian" Patience (模拟)
    UVA 10539 Almost Prime Numbers( 素数因子)
    HDU 1272 小希的迷宫(并查集)
    HDU 1213 How Many Tables (并查集)
    POJ 2236 Wireless Network(并查集)
    HDU 1233 还是畅通工程 ( Kruskal或Prim)
  • 原文地址:https://www.cnblogs.com/puyongsong/p/6260174.html
Copyright © 2011-2022 走看看