将存储在变量中的数据数组传递到 javascript 中的功能混合模式 OOP

passing data arrays stored in variables, into functional mixin pattern OOP in javascript

我有一个 java 脚本 OOP 模式如下:主对象(团队)具有其他对象(玩家)作为属性。 Team 和 Player 对象都存在功能性混合以保留方法。这两个对象的新实例都是使用构造函数创建的,Player 对象作为参数传递给 Team 对象。然后 mixins 调用对象以允许方法可用(此处更全面地描述了一种方法:

https://javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at-javascript-mixins/.

现在,新 Player 的属性是数字数组。如果我在创建新玩家时 "hard code" 中的数字,则一切正常,并且可以在对象上使用这些方法。但是,我希望使数据选择更加动态(例如,数组中的值可能会根据菜单选择等而改变)。出于这个原因,我想将数据数组存储在一个变量中,然后将该变量传递给新的 Player 构造函数序列并仍然保持混合功能。不幸的是,简单地按原样或 [] 符号替换变量名会破坏功能并导致 console.log 请求没有结果或 NaN 结果。

任何人都可以帮助完成这一步...

带有说明性注释的代码如下:

// Mixin for Team methods...

var asTeam = function() {
  this.ability1 = function() {
    var ab = (this.p1.p4arr[4] + this.p2.p5arr[2]);
    return ab;
  };
  return this;
};

// Mixin for Player Methods..The methods use calculations of different values within the property arrays..
var asPlayer = function() {
  this.skill1 = function() {
    var a = this.p4arr[0] + this.p5arr[0];
    return a;
  };
  this.skill2 = function() {
    var a = this.p5arr[2] * this.p6arr[3] / this.p4arr[4];
    return a;
  };
  return this;
};

// constructor function for Teams..
var Team = function(p1, p2, p3) {
  this.p1 = p1;
  this.p2 = p2;
  this.p3 = p3;
};

// constructor function for Players..
var Player = function(p4arr, p5arr, p6arr) {
  this.p4arr = p4arr;
  this.p5arr = p5arr;
  this.p6arr = p6arr;
};

// call the object prototypes from Mixins..
asTeam.call(Team.prototype);
asPlayer.call(Player.prototype);

// create a new Players..

// If used, this code works...

//var john = new Player([3,7,9,4,1,6],[2,16,3,5,1,1],[24,6,3,21,5,3]);
//var bob = new Player([2,2,1,3,4,1],[3,8,6,2,1,4],[6,4,8,6,2,5]);
//var ian = new Player([3,8,3,2,5,4],[2,7,4,3,7,3],[7,4,8,5,3,2]);

// Instead want to use this code or something similar that works to pick up the data arrays from the variables at the bottom ...

var john = new Player(arr1, arr2, arr3);
var bob = new Player(arr4, arr5, arr6);
var ian = new Player(arr7, arr8, arr9);

//or (doesnt work either)

// var john = new Player([arr1],[arr2],[arr3]);
//etc..

// create a new Team
var Team1 = new Team(john, bob, ian);

console.log(Team1.ability1());
console.log(john.skill2());
console.log(bob.skill1());
console.log(john.p4arr);

// data arrays stored in variables..

var arr1 = [3, 7, 9, 4, 1, 6];
var arr2 = [2, 16, 3, 5, 1, 1];
var arr3 = [24, 6, 3, 21, 5, 3];

var arr4 = [2, 2, 1, 3, 4, 1];
var arr5 = [3, 8, 6, 2, 1, 4];
var arr6 = [6, 4, 8, 6, 2, 5];

var arr7 = [3, 8, 3, 2, 5, 4];
var arr8 = [2, 7, 4, 3, 7, 3];
var arr9 = [7, 4, 8, 5, 3, 2];

您在初始化变量之前调用构造函数,因此您将未定义的值传递给构造函数。只需更改代码的顺序:

// data arrays stored in variables..

var arr1 = [3, 7, 9, 4, 1, 6];
var arr2 = [2, 16, 3, 5, 1, 1];
var arr3 = [24, 6, 3, 21, 5, 3];

var arr4 = [2, 2, 1, 3, 4, 1];
var arr5 = [3, 8, 6, 2, 1, 4];
var arr6 = [6, 4, 8, 6, 2, 5];

var arr7 = [3, 8, 3, 2, 5, 4];
var arr8 = [2, 7, 4, 3, 7, 3];
var arr9 = [7, 4, 8, 5, 3, 2];

var john = new Player(arr1, arr2, arr3);
var bob = new Player(arr4, arr5, arr6);
var ian = new Player(arr7, arr8, arr9);

// create a new Team
var Team1 = new Team(john, bob, ian);

console.log(Team1.ability1());
console.log(john.skill2());
console.log(bob.skill1());
console.log(john.p4arr);