在我的代码中,我希望能够在Game
下创建可以这样调用的函数:
Game.Player.iJava.showNum()
哪个会回归' 5'
和
Game.Player.Bill.showNum()
哪个会返回' 120'
我对语法很困惑,能够像这样调用它,这是我到目前为止所拥有的 很抱歉,如果我的描述含糊不清,我会尽力解释这一点,但我的英语不是很好
谢谢,这是我目前所拥有的
var Username = 'iJava';
function Game(Object, Attribute, Number) {
this.Object = Object;
this.Attribute = Attribute;
this.Number = Number
this.showNum= function() { return Number;}
this.Create = function() { if (Object == "Player") { console.log("Created user "+Attribute) } };
}
function Player(Name, Num) {
new Game("Player", Name, Num);
}
var User = new Player(Username, 5)
var Bill = new Player("Bill", 120)
答案 0 :(得分:1)
您可以将函数以及任何内容作为其他对象的属性:
function Game() { ... }
function Player() { ... }
Game.Player = Player;
Game.Player.Bill = new Game.Player("Bill", 120);