javascript - 是否可以将值与构造函数的参数进行比较?

标签 javascript html constructor

    function startMatch(){
      Wbrook0 = new component(50, 50, "pieces/WhiteBrook.png", 0, 0, "piece", "Wbro0");
      Wknight0 = new component(50, 50, "pieces/WhiteKnight.png", 50, 0, "piece", "Wknight0");
      Wbishop0 = new component(50, 50, "pieces/WhiteBishop.png", 100, 0, "piece", "Wbis0");
      Wqueen = new component(50, 50, "pieces/WhiteQueen.png", 150, 0, "piece", "Wque");
      Wking = new component(50, 50, "pieces/WhiteKing.png", 200, 0, "piece", "Wking");
      Wbishop1 = new component(50, 50, "pieces/WhiteBishop.png", 250, 0, "piece", "Wbis1");
      Wknight1 = new component(50, 50, "pieces/WhiteKnight.png", 300, 0, "piece", "Wknight1");
      Wbrook1 = new component(50, 50, "pieces/WhiteBrook.png", 350, 0, "piece", "Wbro1");
    }

    function helpme(){
      console.log(component.length); //This will output 7, which is how many parameters component has
      console.log(Bking.constructor.length); //Same here
    }

    function component(width, height, color, x, y, type, me){ 
      //Mostly unrelated stuff, the stuff I left just defines the parameters
      this.width = width;
      this.height = height;
      this.x = x;
      this.y = y;
      this.id = me;
    }

是否可以找到所有组件参数的所有当前值? (即,如果我将“x”插入到 console.log() 中询问的代码中,它将打印 0、50、100、150、200、300 和 350。

我知道我可以为每一个执行 console.log(Wbrook.x) ,但我有 32 个,为了做我想做的事,我还需要执行 y 和 id ,然后在我之前把它弄得一团糟,我想看看是否有更简单的方法。

如果描述和标题含糊不清,我深表歉意,我对构造函数还没有太多经验。提前致谢。

最佳答案

在实例化组件时,我会将组件保存在数组中。然后,您可以查询该数组并选择所需的属性。

_allComponents = [];

function startMatch() {
  Wbrook0 = new component(50, 50, "pieces/WhiteBrook.png", 0, 0, "piece", "Wbro0");
  Wknight0 = new component(50, 50, "pieces/WhiteKnight.png", 50, 0, "piece", "Wknight0");
  Wbishop0 = new component(50, 50, "pieces/WhiteBishop.png", 100, 0, "piece", "Wbis0");
  Wqueen = new component(50, 50, "pieces/WhiteQueen.png", 150, 0, "piece", "Wque");
  Wking = new component(50, 50, "pieces/WhiteKing.png", 200, 0, "piece", "Wking");
  Wbishop1 = new component(50, 50, "pieces/WhiteBishop.png", 250, 0, "piece", "Wbis1");
  Wknight1 = new component(50, 50, "pieces/WhiteKnight.png", 300, 0, "piece", "Wknight1");
  Wbrook1 = new component(50, 50, "pieces/WhiteBrook.png", 350, 0, "piece", "Wbro1");
}

function helpme() {
  console.log(component.length); //This will output 7, which is how many parameters component has
  console.log(Bking.constructor.length); //Same here
}

function component(width, height, color, x, y, type, me) { //Mostly unrelated stuff, the stuff I left just defines the parameters
  this.width = width;
  this.height = height;
  this.x = x;
  this.y = y;
  this.id = me;
  _allComponents.push(this);
}

function getPropertyOfComponents(components, propertyName) {
  var values = [];
  components.forEach(function(component) {
    values.push(component[propertyName]);
  });
  return values.join(",");
}

startMatch();
console.log(getPropertyOfComponents(_allComponents, 'x'));

关于javascript - 是否可以将值与构造函数的参数进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40622768/

相关文章:

javascript - 为什么 async : false inside a listener, 停止所有执行?

javascript - 添加另一个级别 - 侧边菜单 HTML/Bootstrap 3/JS

html - background-size 与元素有什么关系?

javascript - AngularJS 指令 templateUrl 未加载到 View 中

java - 创建复制构造函数和覆盖的 clone() 方法

javascript检查链接内的文本 anchor 文本是否溢出

javascript - 重定向自动完成

jquery - 函数不起作用,更改属性和添加/删除类 jquery

java - 扩展 fragment 的空构造函数

C# - 来自动态对象的隐式构造函数