es6标准入门
let const 解构 字符串模板 模板字符串(template string)是增强版的字符串,用反引号(`)标识。它可以当作普通字符串使用,也可以用来定义多行字符串,或者在字符串中嵌入变量。 如果使用模板字符串表示多行字符串,所有的空格和缩进都会被保留在输出之中。
1 2 3 4 5 6 7 8 const a = 'hello' const b = 'world' const c = `foo ${a} ${b} bar` console .log(c)console .log(c.startsWith("foo" ))
…values的运用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 const a = 'hello' const b = 'world' const c = test `foo ${a} ${b} bar` function test (strs, ...values ) { console .log(strs,values) }
下面代码中,所有模板字符串的空格和换行,都是被保留的,比如
标签前面会有一个换行。如果你不想要这个换行,可以使用trim方法消除它。
1 2 3 4 5 6 $('#list' ).html(` <ul> <li>first</li> <li>second</li> </ul> ` .trim());
大括号内部可以放入任意的 JavaScript 表达式,可以进行运算,以及引用对象属性。 模板字符串之中还能调用函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 let x = 1 ;let y = 2 ;`${x} + ${y} = ${x + y} ` `${x} + ${y * 2 } = ${x + y * 2 } ` let obj = {x : 1 , y : 2 };`${obj.x + obj.y} ` function fn ( ) { return "Hello World" ; } `foo ${fn()} bar`
对象和数组 数组 1 2 3 4 const s = "😺😄🙄👌" const r = Array .from(s)console .log(r)
字符串拼接, …拓展运算符
1 2 3 4 5 6 7 8 9 10 11 const s = "😺😄🙄👌" const t = ["🌺" ,"🍊" ,...s]console .log(t)
对象 1 2 3 4 5 6 7 8 9 10 11 12 13 14 const s = "😺😄🙄👌" const t = ["🌺" ,"🍊" ,...s]const k = 'arr' const r = { [k+1 ]:'1' , s, t } console .log(r)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 const eat = { getEat(){ return "🍗" } } const drink = { getDrink(){ return "🍺" } } let sunday = Object .create(eat)console .log(sunday.getEat())console .log(Object .getPrototypeOf(sunday))console .log(Object .setPrototypeOf(sunday, drink))console .log(sunday.getDrink())
对象有proto 属性,函数有prototype属性;
对象由函数生成;
生成对象时,对象的proto 属性指向函数的prototype属性。 在没有手动修改proto 属性的指向时,以上三条便是JavaScript默认原型链指向逻辑。
参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 const eat = { getEat(){ return "🍗" } } const drink = { getDrink(){ return "🍺" } } let sunday = { __proto__: eat } console .log(sunday.getEat())sunday.__proto__ = drink console .log(sunday.getDrink())
1 2 3 4 5 6 7 8 9 10 11 12 13 const eat = { getEat(){ return "🍗" } } let sunday = { __proto__: eat, getEat(){ return super .getEat() + "🍔" } } console .log(sunday.getEat())
函数 1 2 3 4 5 const fn = function p ( ) {} console .log(fn.name)
箭头函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 (() => { console .log('hello' ) })() const r = [1 ,2 ,3 ].map(function (i ) { return i*3 }) console .log(r)const r = [1 ,2 ,3 ].map((i )=> i*3 )console .log(r)
在调用的一瞬间,this就被绑定到s上了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 const s = { a: 40 , p: function ( ) { const q = { a: 50 , test: () => { console .log(this .a) } } q.test() } } s.p()
函数是唯一可以影响this指向的因素,而箭头函数不影响this的指向。this在箭头函数中已经按照词法作用域绑定了,用call()或者apply()调用箭头函数时,无法对this进行绑定。参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 window .a = 50 const s = { a: 40 , p: function ( ) { console .log(this .a) } } s.p() window .a = 50 const s = { a: 40 , p: () => { console .log(this .a) } } s.p()
传参
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 function test ({options=true}={} ) { console .log(options) } test({options :{a :1 ,b :2 }}) function test (...options ) { console .log(options) } test(30 ,{options :{a :1 ,b :2 }})