<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script> function Hero(name, blood, weapon) { // 實例成員 / 對象成員 -- 跟對象相關的成員,將來使用對象的方式來調用 this.name = name; this.blood = blood; this.weapon = weapon; this.attack = function () { console.log(this.weapon + ' 攻擊敵人'); } } // 靜態成員 -- 直接給構造函數添加的成員 Hero.version = '1.0'; var hero = new Hero('劉備', 100, '劍'); hero.attack(); var hero1 = new Hero('關羽', 100, '刀'); hero1.attack(); // 靜態成員不能使用對象的方式來調用 console.log(hero.version); // 靜態成員使用構造函數來調用 console.log(Hero.version); </script> </body> </html>
