简体   繁体   English

如何获取从 function 构造函数创建的所有新对象名称?

[英]How can I get all new objects name created from a function constructor?

I am working on a javaScript project, building a turned based game, and I have created a function constructor for the Players.我正在开发一个 javaScript 项目,构建一个基于回合的游戏,并且我为玩家创建了一个 function 构造函数。

I added a css class for each player, with the same name of the players.我为每个播放器添加了一个 css class,与播放器的名称相同。

Below I added my js code:下面我添加了我的js代码:

function Player(name, image) {
    this.name = name;
    this.image = image;
}

var player1 = new Player("kakashi", "ninja.png");
var player2 = new Player("mightyGuy", "samurai.png");

function checkClass() {
        $.each(classList, function(index, cssClass) {
            if (
                cssClass === "wall" ||
                cssClass === "kakashi" ||
                cssClass === "mightyGuy"
            ) {
                blocked = true;
            }
        });
    }

How could I get all new player.name created from this constructor in order to check if a specific divCell has any player.name class??我如何获取从该构造函数创建的所有新 player.name 以检查特定 divCell 是否有任何 player.name class?

So instead of write cssClass === "kakashi" ||所以不要写 cssClass === "kakashi" || cssClass === "mightyGuy" cssClass === "mightyGuy"

I would simply write it one time, and automatically it will check all css classes by Player.name.我会简单地写一次,它会自动按 Player.name 检查所有 css 类。

Update question: This is the way I end up using in my code:更新问题:这是我最终在我的代码中使用的方式:

function Player(name, image) {
    this.name = name;
    this.image = image;
}

var player1 = new Player("kakashi", "ninja.png");
var player2 = new Player("mightyGuy", "samurai.png");

function checkClass() {
    $.each(classList, function(index, cssClass) {
        if (
            cssClass === "wall" ||
            cssClass == passivePlayer.name
        ) {
            blocked = true;
        }
    });
}

I used this way because I don't need to write any player name by using the var passivePlayer (that could be any player except the activePlayer)我使用这种方式是因为我不需要使用 var passivePlayer 来写任何玩家名称(可以是除了 activePlayer 之外的任何玩家)

you can add the classes to an object and check if they exist.您可以将类添加到 object 并检查它们是否存在。

function Player(name, image) {
    this.name = name;
    this.image = image;
}

var player1 = new Player("kakashi", "ninja.png");
var player2 = new Player("mightyGuy", "samurai.png");
function checkClass() {
    const checkList = {
        wall: true,
        kakashi: true,
        mightyGuy: true
    }

    $.each(classList, function (index, cssClass) {
        if (checkList[cssClass]) {
            blocked = true;
        }
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以访问使用 object 构造函数 function 创建的多个对象的参数吗? - Can I access the parameters of multiple objects created with object constructor function? 我如何从此函数构造函数获取全名 - How do i get the full name from this function constructor 获取使用Function构造函数创建的函数的名称 - Get name of function created using Function constructor 如何在函数中获取新对象的构造函数的名称? (JavaScript)的 - How to get the name of the new object's constructor in the function? (Javascript) 获取自定义对象的构造函数名称 - get custom objects constructor function name 如何访问数组对象并使用新创建的 function 再次调用它们? 使用有问题的代码示例 - How can i access the array objects and call them again with new created function? Using the example of problematic codes 如何一次从同一构造函数调用所有对象? - How do I call all objects from the same constructor function at once? 我如何从数组中获取两个值并将它们包含在angularjs控制器中的新函数中 - How can I get two values from an array and include them in a new function all in an angularjs controller 什么时候在JavaScript的构造函数和类中创建新对象? - When are new objects created in JavaScript's constructor function vs in class? 使用Javascript中使用相同函数构造函数创建的所有对象实例的数组 - Array with all Instances of Objects created with the same function constructor in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM