<script>
function Img(tag,attr,style,fa){ //创建一个构造函数,传入对应形参
this.newele = document.createElement(tag); //创建标签节点
this.attr = attr; //接收属性
this.style = style; //接收样式
this.fa = fa || document.body; // 设置父节点,若没有传入形参,则默认为document.body
this.setAttr(); //设置属性
}
Img.prototype.setAttr = function(){ //在原型对象中设置属性方法
Object.assign(this.newele,this.attr); //利用对象合并方法,将对象合并为一个
this.style && this.setStyle(); //判断,若传入样式,则设置样式
}
Img.prototype.setStyle = function(){ //在原型对象中设置样式方法
Object.assign(this.newele.style,this.style); //利用对象合并方法,将对象合并为一个
this.append(); //追加节点
}
Img.prototype.append = function(){ //在原型对象中设置追加节点方法
this.fa.appendChild(this.newele);
}
//实例化对象
new Img(
'img',
{src:'../images/1.jpg'},
{width:'300px',height:'400px'}
);
</script>