<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.padding {
padding: 50px;
}
</style>
</head>
<body>
<div class="padding">
</div>
<script>
(function(){
var Ajax=function(){
var Ajax= function (){
this.success;
this.statesuc=false;
this.statefail=false;
this.fail;
this.init();
}
Ajax.prototype={
init:function(){
var rnd= Math.random()*5;
if(rnd>2){
this.success=rnd
this.statesuc=true;
}else{
this.fail=rnd;
this.statefail=true;
}
return this
},
then:function(callback){
if(this.statesuc){
callback&&callback(this.success)
console.log(‘大于2‘+this.success);
}
return this
},
catch:function(callback){
if(this.statefail){
callback&&callback(this.fail)
console.log(‘小于2‘+this.fail);
}
}
}
return new Ajax()
}
window.Ajax=Ajax
})()
Ajax().then(function(res){
console.log(‘成功‘+res)
}).catch(function(ret){
console.log(‘失败‘+ret)
})
</script>
</body>
</html>
原文地址:https://www.cnblogs.com/zzh965390267/p/9307846.html