面向对象的使用

    xiaoxiao2021-12-15  31

    <!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>
    转载请注明原文地址: https://ju.6miu.com/read-1000033.html

    最新回复(0)