星空网 > 软件开发 > 网页设计

es6笔记1^_^let、string、number、math

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

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

  <!-- 加载Traceur编译器 -->  <script src='/images/loading.gif' data-original="http://google.github.io/traceur-compiler/bin/traceur.js" type="text/javascript"></script>  <script src='/images/loading.gif' data-original="https://google.github.io/traceur-compiler/bin/BrowserSystem.js"></script>  <!-- 将Traceur编译器用于网页 -->  <script src='/images/loading.gif' data-original="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)的。反斜线也不再是特殊字符,\n 也不会被解释成换行符:
  if(1){    let raw=String.raw`hi : \n`;    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) // trueNumber.isInteger(25.0) // trueNumber.isInteger(25.1) // falseNumber.isInteger("15") // falseNumber.isInteger(true) // false

六、Math

  Math对象新增的方法,都是静态方法,只能在Math对象上调用。
  Math.trunc():去除一个数的小数部分,返回整数部分。
Math.trunc(4.1) // 4Math.trunc(-4.1) // -4

注意:对于空值和无法截取整数的值,返回NaN。
Math.sign():判断一个数到底是正数、负数、还是零。
返回五种值:参数为正数,返回+1;参数为负数,返回-1;参数为0,返回0;参数为-0,返回-0;其他值,返回NaN。
Math.sign(-5) // -1Math.sign(5) // +1Math.sign(0) // +0Math.sign(-0) // -0Math.sign('hubwiz'); // NaN//Math.cbrt:计算一个数的立方根。Math.cbrt(-1); // -1Math.cbrt(0); // 0Math.cbrt(2); // 1.2599210498948732//Math.fround:返回一个数的单精度浮点数形式。Math.fround(0);   // 0Math.fround(1.337); // 1.3370000123977661Math.fround(NaN);  // NaN//Math.hypot:返回所有参数的平方和的平方根。Math.hypot(3, 4);    // 5Math.hypot(3, 4, 5);   // 7.0710678118654755Math.hypot();      // 0Math.hypot(NaN);     // NaNMath.hypot(3, 4, 'foo'); // NaNMath.hypot(3, 4, '5');  // 7.0710678118654755Math.hypot(-3);     // 3//如果参数不是数值,Math.hypot方**将其转为数值。只要有一个参数无法转为数值,就会返回NaN。/*8.Math 对数方法Math.expm1(x):返回ex - 1。*/Math.expm1(-1); // -0.6321205588285577Math.expm1(0); // 0Math.expm1(1); // 1.718281828459045//Math.log1p(x):返回1 + x的自然对数。如果x小于-1,返回NaN。Math.log1p(1); // 0.6931471805599453Math.log1p(0); // 0Math.log1p(-1); // -InfinityMath.log1p(-2); // NaN//Math.log10(x):返回以10为底的x的对数。如果x小于0,则返回NaN。Math.log10(2);   // 0.3010299956639812Math.log10(1);   // 0Math.log10(0);   // -InfinityMath.log10(-2);   // NaNMath.log10(100000); // 5//Math.log2(x):返回以2为底的x的对数。如果x小于0,则返回NaN。Math.log2(3);  // 1.584962500721156Math.log2(2);  // 1Math.log2(1);  // 0Math.log2(0);  // -InfinityMath.log2(-2);  // NaNMath.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)

 此篇笔记整合:

es6笔记1^_^let、string、number、mathes6笔记1^_^let、string、number、math
<!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)的。反斜线也不再是特殊字符,\n 也不会被解释成换行符:*/  if(1){    let raw=String.raw`hi : \n`;    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) // 4Math.trunc(-4.1) // -4/*注意:对于空值和无法截取整数的值,返回NaN。Math.sign():判断一个数到底是正数、负数、还是零。返回五种值:参数为正数,返回+1;参数为负数,返回-1;参数为0,返回0;参数为-0,返回-0;其他值,返回NaN。*/Math.sign(-5) // -1Math.sign(5) // +1Math.sign(0) // +0Math.sign(-0) // -0Math.sign('hubwiz'); // NaN//Math.cbrt:计算一个数的立方根。Math.cbrt(-1); // -1Math.cbrt(0); // 0Math.cbrt(2); // 1.2599210498948732//Math.fround:返回一个数的单精度浮点数形式。Math.fround(0);   // 0Math.fround(1.337); // 1.3370000123977661Math.fround(NaN);  // NaN//Math.hypot:返回所有参数的平方和的平方根。Math.hypot(3, 4);    // 5Math.hypot(3, 4, 5);   // 7.0710678118654755Math.hypot();      // 0Math.hypot(NaN);     // NaNMath.hypot(3, 4, 'foo'); // NaNMath.hypot(3, 4, '5');  // 7.0710678118654755Math.hypot(-3);     // 3//如果参数不是数值,Math.hypot方**将其转为数值。只要有一个参数无法转为数值,就会返回NaN。/*8.Math 对数方法Math.expm1(x):返回ex - 1。*/Math.expm1(-1); // -0.6321205588285577Math.expm1(0); // 0Math.expm1(1); // 1.718281828459045//Math.log1p(x):返回1 + x的自然对数。如果x小于-1,返回NaN。Math.log1p(1); // 0.6931471805599453Math.log1p(0); // 0Math.log1p(-1); // -InfinityMath.log1p(-2); // NaN//Math.log10(x):返回以10为底的x的对数。如果x小于0,则返回NaN。Math.log10(2);   // 0.3010299956639812Math.log10(1);   // 0Math.log10(0);   // -InfinityMath.log10(-2);   // NaNMath.log10(100000); // 5//Math.log2(x):返回以2为底的x的对数。如果x小于0,则返回NaN。Math.log2(3);  // 1.584962500721156Math.log2(2);  // 1Math.log2(1);  // 0Math.log2(0);  // -InfinityMath.log2(-2);  // NaNMath.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

 此篇终,待续……











原标题:es6笔记1^_^let、string、number、math

关键词:string

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

了解欧盟注册商标的证书类型:https://www.kjdsnews.com/a/1360177.html
了解欧盟注册商标的认证证书:https://www.kjdsnews.com/a/1360178.html
了解欧盟商标的类别和申请要求:https://www.kjdsnews.com/a/1360179.html
了解欧盟商标申请的必要条件:https://www.kjdsnews.com/a/1360180.html
了解欧盟商标注册费用及流程,让个人商标注册更轻松:https://www.kjdsnews.com/a/1360181.html
了解欧盟商标注册的费用标准及流程:https://www.kjdsnews.com/a/1360182.html
宠物梳专利查询分析:https://www.kjdsnews.com/a/1842293.html
温州旧货市场有玻璃柜卖吗?:https://www.vstour.cn/a/411246.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流