js面向对象的编程
基本结构,原生的
function pictureCompression(obj){
this.el = obj.image;
this.eleFile=this.el.querySelector(".file");
this.originWidth = null;
}
pictureCompression.prototype={
constructor:pictureCompression, //构造函数
init:function(){//主方法
this.imgCompressChange();
},
//计算图片压缩比
getCompressRate:function(allowCompressRate){ //计算压缩比率,size单位为KB
var compressRate = 1;
if(allowCompressRate === 3){
compressRate = 0.5;
} else if(allowCompressRate === 2){
compressRate = 0.7;
} else if(allowCompressRate === 1){
compressRate = 0.92;
}
return compressRate;
},
}
var imageCompress=document.getElementsByClassName("image-compress");
var compression=new pictureCompression({
imageCompress:imageCompress[0],
maxWidth:800,
maxHeight:800,
allowCompressRate:1,
})
在外面操作数据,用实例对象
upLoad.onclick=function(){
console.log(compression.compressArr);
}
es6,的类
class pictureCompression{
constructor(){}
init:function(){//主方法
this.imgCompressChange();
}
//计算图片压缩比
getCompressRate:function(allowCompressRate){ //计算压缩比率,size单位为KB
var compressRate = 1;
if(allowCompressRate === 3){
compressRate = 0.5;
} else if(allowCompressRate === 2){
compressRate = 0.7;
} else if(allowCompressRate === 1){
compressRate = 0.92;
}
return compressRate;
}
}
let pictureCompression = new pictureCompression();