技术码头

ES6 基本用法

2020-01-08  本文已影响0人  石菖蒲_xl

let

function add(){
    console.log(a); // ReferenceError: Cannot access 'a' before initialization
    let a = 1;
    console.log(a); // => 1
    if(a===1){
        let b = 2;
        console.log(b); // => 2
    }
    console.log(b); // ReferenceError: b is not defined
}
add()
var tmp = 123;
if (true) {
  console.log(tmp);  // ReferenceError: Cannot access 'tmp' before initialization
  let tmp;
}

ES6 明确规定,如果区块中存在letconst命令,这个区块对这些命令声明的变量,从一开始就形成了封闭作用域,凡是在声明之前就使用这些变量,就会报错。

function bar(x = y, y = 2) {
  return [x, y];
}
bar(); // 报错

参数x默认值等于另一个参数y,而此时y还没有声明,属于“死区”。

// 报错
function func() {
  let a = 10;
  var a = 1;
}

// 报错
function func() {
  let a = 10;
  let a = 1;
}
// 不能在函数内部重新声明参数。
function func(arg) {
  let arg;
}
func() // 报错

function func(arg) {
  {
    let arg;
  }
}
func() // 不报错

ES6 的块级作用域

如下代码如果使用var定义变量,会输出10,但是块级作用域外层代码块不受内存代码块影响

function f () {
  let n = 5;
  if ( true ) {
    let n = 10;
  }
  console.log(n); // 5
}

const


顶层对象的属性

顶层对象在浏览器环境指的是window对象,在Node 指的是global对象。

window.a = 1;
a ; // => 1

a = 2;
window.a; // => 2

上边代码中,顶层对的属性赋值与全局变量的赋值,是同一件事。

let b = 1;
window.b; // undefined

数组的解构赋值

let a = 1;
let b = 2;
let c = 3;
// ES6 允许写成下面这样
let [a,b,c] = [1,2,3];
let [x,,y] = [1,2,3];
x // => 1,
y // => 3

let [x,...y] = [1,2,3,4];
x; // => 1
y; // => [2,3,4]

let [x, y, ...z] = ['a'];
x // "a"
y // undefined
z // []
let [a] = [];
let [b,a] = [1];
// a 的值都是undefined
let [a = 1] = [];
a; // => 1
let [a = 1] = [undefined];
a; // => 1
let [b = 1] = [null];
b; // => null   因为null不严格等于undefined
function f(){
  console.log('aaaa');
}
let [a = f()] = [1];
a; // => 1 因为a能取到值,所以f根本不会执行

对象的解构赋值

对象的解构与数组不同,数组的元素是按次序排列的,变量的取值由它的位置决定,而对象的属性没有次序,变量必须与属性同名,才能取到正确的值

let { b , a } = { a:1 , b:2 };
a; // => 1
b; // => 2 
let { c } = { a:1 , b:2 };
c; // => undefined  解构失败,变量的值等于undefined
let obj = {first:"hello",last:"world"};
let {first:h,last:w} = obj;
h; // => 'hello'
w: // => 'world'
let o1 = {
   x: 1
};
let o2 = Object.create(o1);
o2.y = 2;
const { x , y } = o2;
x; // => 1
y; // => 2
let { x = 1 } = {};
x; // => 1

let { x: y = 2 } = {};
y; // => 2
let { x:1 } = { x:undefined };
x; // => 1

let { y:2 } = { y:null };
y; // null

字符串的解构赋值

字符串也可以解构赋值,这是因为字符串会被转成一个类似数组的对象。

const [a, b, c, d, e] = 'hello';
a // "h"
b // "e"
c // "l"
d // "l"
e // "o"
//类似数组的对象都有一个length属性,因此还可以对这个属性解构赋值
let {length : len} = 'hello';
len // 5

模板字符串

const value = 'o';
let str = `hell${value} world`;

includes()、startsWith()、endsWith()

三个方法都是用来确定一个字符串是否包含在另一个字符串中

let str = 'hello world!';
str.includes('el'); // => true
str.startsWith('he');// => true
str.startsWith('e'); // => false
str.endsWith('d!'); // => true
str.endsWith('d'); // => false
str.includes('lo',4); // => false
str.startsWith('lo',3); // => true 相当于指定开始位置
str.endsWith('hel',3); // => true 表示前三个字符是存在的

实例方法:repeat()

'x'.repeat(3); // => 'xxx'
'x'.repeat(0); // => ''
'x'.repeat(2.9); // => 'xx'
'x'.repeat(-1); // 报错
'x'.repeat(-0.2); // => '' 参数认为是0
'x'.repeat(NaN); // => ''  
'x'.repeat('aaa'); // => ''  
'x'.repeat('3'); => 'xxx'

如上:'aaa'转成数字是NaNNaN等同于0,所以输出空字符串


实例方法:padStart()、padEnd()

字符串补全功能,如果某个字符串不够指定长度,会在头部或尾部补全。

'x'.padStart(5, 'ab') // 'ababx'
'x'.padEnd(5, 'ab') // 'xabab'

'1'.padStart(4, 'lx') // 'lxl1'

实例方法:trimStart()、trimEnd()

ES2019对字符串实例新增了trimStart()trimEnd()这两方法。

const a = ' ab ';
s.trim(); // => "ab"
s.trimStart(); // => "ab "
s.trimEnd(); // => " ab"
上一篇下一篇

猜你喜欢

热点阅读