<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<script>
//升级普通版
function Sprite(option){
this.name = option.name;
this.age = option.age;
}
//在原型中添加方法
Sprite.prototype.show = function(){
console.log(this.name);
}
var s = new Sprite({
name:'222',
age:20
});
s.show();
//最终版方法
function Sprite(option){
this._init(option);
}
Sprite.prototype = {
_init:function(option){
this.name = option.name;
this.age = option.age;
this.color = 'red';
},
show:function(){
console.log('ss');
}
};
var s = new Sprite({
name:'sss',
age:19
});
s.show();
</script>
</body>
</html>
面向对象的使用
最新推荐文章于 2023-04-08 19:48:37 发布