gpt4 book ai didi

JavaScript 类语法?

转载 作者:行者123 更新时间:2023-12-03 11:43:40 25 4
gpt4 key购买 nike

在我的代码中,我希望能够在 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)

最佳答案

您可以将函数以及任何内容作为其他对象的属性:

function Game() { ... }
function Player() { ... }

Game.Player = Player;
Game.Player.Bill = new Game.Player("Bill", 120);

关于JavaScript 类语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26151489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com