es6部分知识-章节2

1.字符串

>1.多了两个新方法

       startsWith

let str="http://nb350.com";
if(str.startsWith("http://")){
    alert("普通网址")
}else if(str.startsWith("https://")){
    alert("加密网址")
}else if(str.startsWith("git://")){
    alert("git网址")
}else if(str.startsWith("svn://")){
    alert('svn地址')
}else{
    alert("其他")
}

    endsWith 

let str='1.txt';
if(str.endsWith('.txt')){
	alert("文本");
}else{
	alert("其他");
}

2.字符串模板

  字符串连接

let c='abc';
let str=`<a>${c}</a>`;
//i.直接把东西塞到字符串里面
//ii.可以拆行

3.面对对象-基础

/*
es6的面向对象
1.class关键字、构造器和类分开了
2.class 里边直接加方法
*/

//老版的面对对象
/*function User(name,pass){
	console.log(this);
	this.name=name;
	this.pass=pass;
}
User.prototype.showName=function(){
	alert(this.name);
}
User.prototype.showPass=function(){
	alert(this.pass);
}

var u1=new User('lyj','123456');
u1.showName();
u1.showPass();*/
//新版面向对象

class User{
	constructor(name,pass){
		this.name=name;
		this.pass=pass;
	}

	showName(){
		alert(this.name);
	}

	showPass(){
		alert(this.pass);
	}
}
var u1=new User('lyj','123456');
u1.showName();
u1.showPass();
//老版原型继承
/*function User(name,pass){
	console.log(this);
	this.name=name;
	this.pass=pass;
}
User.prototype.showName=function(){
	alert(this.name);
}
User.prototype.showPass=function(){
	alert(this.pass);
}

function vipUser(name,pass,level){
	User.call(this,name,pass);
	this.level=level;
}
vipUser.prototype=new User();
vipUser.prototype.constructor=vipUser;
vipUser.prototype.showLevel=function(){
	alert(this.level);
}

var v1=new vipUser('1','2',3);

alert(v1.name);
v1.showLevel();*/

class User{
	constructor(name,pass){
		this.name=name;
		this.pass=pass;
	}

	showName(){
		alert(this.name);
	}

	showPass(){
		alert(this.pass);
	}
}

class vipUser extends User{
	constructor(name,pass,level){
		super(name,pass);
		this.level=level;
	}
	showLevel(){
		alert(this.level);
	}
}
let v1=new vipUser('1','2',3);
alert(v1.name);
v1.showLevel();

4.promise

简单使用

var p1=new Promise((resolve, reject)=>{
    setTimeout(function (){
      console.log(1111);
    }, 50);
  });
var p2=new Promise((resolve,reject)=>{
	setTimeout(function (){
      console.log(2222);
    }, 10);
})
Promise.all([p1,p2]).then(posts=>{
	console.log(posts);
}).catch(error=>{

})

这里引用下阮一峰老师es6文章的网址

http://es6.ruanyifeng.com/#docs/promise

猜你喜欢

转载自blog.csdn.net/lyj168997/article/details/82416487
今日推荐