该文是有关es6.0中部分新增的字符串的方法、rest参数、扩展运算符、箭头函数以及类的概念和类的继承的个人见解
字符串的方法
includes():查看字符串中是否包含指定字符串
startwith():查看字符串中是否是以指定字符串开头的
endwith():查看字符串中是否以指定字符串结尾的
他们可以写两个参数,第一个是字符串,第二个是index(从索引为第几位开始)
console.log("hi my name is".includes("my")); // true
console.log("hi my name is".startsWith("hi")); // true
console.log("hi my name is".endsWith("is")); // true
以及模板字符串(就是拼接字符串)
var count = 12121;
console.log(`hah${count}aaa`); // hah12121aaa 就是把${}中的内容当作js代码进行处理 而不是字符串
rest、扩展运算符、箭头函数
var muft = v => v;
console.log(muft(20)); // 20; 只有一个参数和一个返回值时
var muft1 = () => console.log(233333);
muft1(); // 233333 没有参数并且只有一句函数体
var muft2 = () => {var arr = [1,2,3.4];console.log(arr);}
muft2(); // 1,2,3,4 没有参数多条语句
var muft3 = (a,b) => {var arr = [1,2,3,4,a,b];console.log(arr);}
muft3(5,6); // 1,2,3,4,5,6 多个参数和多条语句
和普通函数的区别是箭头函数不能使用关键字new来实例化,this的执行不是看被谁调用,而是看在那里定义,不能使用arguments,但是可以使用rest参数(剩余参数:就是传参后还剩下几个没有被引用的参数)
rest参数
var mf = (a,b,...rest) => {console.log(rest)}; // 这里的 ...就是扩展运算符
mf(1,2,3,4); // [ 3,4 ]
var arr = [1,2,3,4];
var mf1 = (a,b,c,d) => {console.log(a,b,c,d);}
mf1(...arr); // 1 2 3 4
console.log(...[1,25,2,46]);// 1 25 2 46
// 扩展运算符 个人理解如果对数组使用就会将数组打散成一个一个单独的数据,如果用来接
收散落的数据,就会创建一个数组把它们存起来
类及其继承
类(class)这个概念都只存在于强类型语言中,但是es6.0新增了这个概念
class Person{
constructor(name,age){
this.name = name;
this.age = age;
}
say(){
console.log(this.name+":Hi")
}
}
var person = new Person("张三",24);
console.log(person); // Person { name: '张三', age: 24 }
person.say(); // 张三:Hi
class Student extends Person{
constructor(name,age,score){
super(name,age)
this.score = score
}
text(){
console.log(this.name+":text")
}
}
var student = new Student("小明同志",16,66);
console.log(student); // Student { name: '小明同志', age: 16, score: 66 }
student.say(); // 小明同志:Hi
student.text(); // 小明同志:text
// 可以通过super函数来继承他父类的属性, 也可以执行他父类中的方法
//可以参考一下写法来理解super函数
function Person(name,age){
this.name = name;
this.age = age;
}
function Student(name,age,score){
Person.call(this,name,age);
this.score = score;
}
var student = new Student("小明同志",16,66);
console.log(student); // Student { name: '小明同志', age: 16, score: 66 }