【问题标题】:Javascript Undefined Array values not connecting to function? How to run a randomized trait function to make each array contain unique traits?Javascript未定义数组值未连接到函数?如何运行随机特征函数以使每个数组包含独特的特征?
【发布时间】:2018-11-10 11:14:12
【问题描述】:

首先,如果标题含糊不清,我深表歉意,我对 javascript 还是很陌生。

运行此代码时,它应该创建 2 个警报窗口。每个 Bob 的 K 特征之一。当然,现在 bob 2 的所有特征都未定义,因为我不知道如何让 SpawnInnerCharacteristics 函数应用于我的所有人。

我需要弄清楚如何从 innercharacteristics 函数中为 bob2(以及未来的 bob3 和 bob 4 等)分配它们自己的内部特征。为此,我需要 A) 更改“Bob”方面的帮助。在 spawnInnerCharacteristics 函数内部,可以容纳的不仅仅是那个单一的 bob。 B) 为每个新创建的 Bob 重新运行 SpawnInnerCharacteristics 函数,这样它们都可以拥有自己独特的特征。以及为了使这两件事正确发生而我所缺少的任何其他东西。

此外,还有一个可选的第三个问题,我将如何生成随机数量的 bobs(使用我的 SpawnInnerCharacteristics 函数,每个都包含自己的内部特征,而不是复本)?

    function rand(maxvalue) {
      return Math.floor(Math.random() * Math.floor(maxvalue));
    }


    function person(id, c, nrg, e, K_, E_, D_, A_, C_, G_, B_, M_, SM_, V_, S_, T_, X_, H_, Z_,int_){
    this.id = id;
    this.color = c;
    this.nrg = nrg;
    this.ec = e;
    this.K = K_;
    this.E = E_;
    this.D = D_;
    this.A = A_;
    this.C = C_;
    this.G = G_;
    this.B = B_;
    this.M = M_;
    this.SM = SM_;
    this.V = V_;
    this.S = S_;
    this.T = T_;
    this.X = X_;
    this.H = H_;
    this.Z = Z_;
    this.int = int_;
    }

    var Mom = new person(1,"c","nrg","brwn", "K kbr", "E e", "d d", "Ay Ay", "c c", "g g", "B B", "m m", "Ay Ay", "v v", "S S", "Td Td", "X X", "h h", "Z Z", 2);
    var Dad = new person(2,"c","nrg","black", "k kbr", "E E", "d d", "Ay Ay", "c c", "g g", "B B", "m m", "Ay Ay", "v v", "S S", "Td Td", "X X", "h h", "Z Z", 5);
    var Bob = new person(spawnInnerCharacteristics);
    var Bob2 = new person(spawnInnerCharacteristics);

    function spawnInnerCharacteristics(){

            Bob.K = physicalCharacteristics(Mom.K, Dad.K)
            Bob.E = physicalCharacteristics(Mom.E, Dad.E)
            Bob.D = physicalCharacteristics(Mom.D, Dad.D)
            Bob.A = physicalCharacteristics(Mom.A, Dad.A)
            Bob.C = physicalCharacteristics(Mom.C, Dad.C)
            Bob.G = physicalCharacteristics(Mom.G, Dad.G)
            Bob.B = physicalCharacteristics(Mom.B, Dad.B)
            Bob.M = physicalCharacteristics(Mom.M, Dad.M)
            Bob.SM = physicalCharacteristics(Mom.SM, Dad.SM)
            Bob.V = physicalCharacteristics(Mom.V, Dad.V)
            Bob.S = physicalCharacteristics(Mom.S, Dad.S)
            Bob.T = physicalCharacteristics(Mom.T, Dad.T)
            Bob.X = physicalCharacteristics(Mom.X, Dad.X)
            Bob.H = physicalCharacteristics(Mom.H, Dad.H)
            Bob.Z = physicalCharacteristics(Mom.Z, Dad.Z)

            Bob.int = mentalCharacteristics(Mom.int, Dad.int)

            return spawnInnerCharacteristics;

                function physicalCharacteristics(a, b){
                var PA = [a.split(" ") , b.split(" ")];
                var AM = PA[0];
                var AD = PA[1]; 
                var CA = AD[Math.floor(Math.random()*AD.length)];
                var LA = AM[Math.floor(Math.random()*AM.length)];
                return LA + " " + CA;
                }
                function mentalCharacteristics(a, b){
                    return ((a*1 + b*1) /2 + rand(3));
                }

        }


    spawnInnerCharacteristics();
    window.alert("This is bob 1's K characteristic. Every once in a while, if the code is working how I'd like it to, the K's will vary between the two bobs: " + Bob.K);
    window.alert("This is bob 2's K characteristic.: " + Bob2.K);

【问题讨论】:

    标签: javascript arrays function this


    【解决方案1】:

    您的代码中出现了一些问题:

    1) 将函数作为 id 传递在这里没有什么意义:

     var Bob = new person(spawnInnerCharacteristics);
    

    2) spawnInnerCharacteristics 始终根据MomDad 设置Bob 的属性。

    3) 虽然不遵循命名约定“不是错误”,但如果您将构造函数大写 (Person) 并将所有其他内容命名为 camelCase (bob, dad )。

    要使这种动态化,您可以将两个父母和孩子作为参数传递给函数:

     function spawnInnerCharacteristics(child, mom, dad) {
       child.k = physicalCharacteristics(mom.k, dad.k);
       //...
     }
    

    这样您就可以轻松创建多个人员并传递他们:

     const mom = new Person(/*...*/);
     const dad = new Person(/*...*/);
    
     const alice = new Person(/*...*/);
     const bob = new Person(/*...*/);
    
     spawnInnerCharacteristics(alice, mom, dad);
     spawnInnerCharacterisitcs(bob, mom, dad);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-12
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2019-09-14
      相关资源
      最近更新 更多