Object.prototype函数错误

时间:2018-04-05 01:12:29

标签: javascript prototype

我正在进行甲板建设挑战,一切都有效,直到创建了一个'新的Deck()'。原型函数在测试中抛出“非函数”错误的最大问题。不确定我是否错误地设置了原型,但是挑战的作者将Deck.prototype设置为具有多种方法的对象。除非被调用为Deck.prototype.draw(),否则带参数的方法不起作用。目标是在代码之下。

以下是我的代码:

  let globalStore = {
    deck: [],
    drawDeck: []
  }

  function Deck(cards) {
    let deck = [];
    // ...
    if(Array.isArray(cards)){
      cards.forEach(card => deck.push(card))
    } else {

      // let drawDeck = [];
    //   if(cards) {
    //     let temp = cards.toString().split(',');
    //     temp.forEach(card => deck.push(card))
    //   }
      for(let i = 0; i < arguments.length; i++){
        deck.push(arguments[i]);
      }
    }
    console.log("Deck: " + deck + "\n")

    globalStore.deck = deck;
    // this.deck = deck;
    // this.drawDeck = drawDeck;
  }

  Deck.prototype = {
    shuffle: function() {
      // Deck.call(this, deck);
      let newDeck = [];
      // const length = this.deck.length;
      const length = globalStore.deck.length;

      for(let i = 0; i < length; i++) {
        let random = Math.floor(Math.random() * globalStore.deck.length);
        let temp = globalStore.deck.splice(random, 1);
        newDeck.push(temp);
      }

      // Deck(newDeck.join(","));
      globalStore.deck = newDeck;

      console.log("New Deck: " +  globalStore.deck + "\n" )
      return globalStore.deck;

    },

    /*
    draw
    @params
      number- number of cards to be drawn
      from- 1 equals top of deck and down, -1 equals bottom of deck and up
    */

    draw: function(number, from) {
      // Deck.call(this, deck)
      globalStore.drawDeck = [];
      if(globalStore.deck.length === 0) {
        return [];
      }
      let drawDeck = globalStore.drawDeck;
      if(number > globalStore.deck.length) {
        number = globalStore.deck.length;
        console.log("Ran out of cards in the deck.");
      }
      if(number === undefined) {
        //draw from top deck
        drawDeck.push(globalStore.deck.shift());
        // Deck(this.deck.join(","));
        console.log("Draw: " + number + "\n...Deck: " + globalStore.deck + "\n...Draw Deck: " + drawDeck + "\n");
      } else if(!parseInt(number) || number <= 0) {
        throw 'Number must be a number greater than 0';
      } else {
        if(from === 1) {
          for(let i = 0; i < number; i++) {
            drawDeck.push(globalStore.deck.shift());
          }
          // Deck(this.deck.join(","));
          console.log("Draw: " + number + " From: " + from + "\n...Deck: " + globalStore.deck + "\n...Draw Deck: " + drawDeck + "\n");
        } else if(from === -1) {
          for(let i = 0; i < number; i++) {
            drawDeck.push(globalStore.deck.pop());
          }
          // Deck(this.deck.join(","));
          console.log("Draw: " + number  + " From: " + from + "\n...Deck: "  + globalStore.deck + "\n...Draw Deck: " + drawDeck + "\n");
        } else if((from !== 1 || from !== -1) && from !== undefined) {
          throw "Second argument to draw must be eith 1 or -1. You wrote: " + from;
        } else {
          for(let i = 0; i < number; i++) {
            drawDeck.push(globalStore.deck.shift());
          }
          // Deck(this.deck.join(","));
          console.log("Draw: " + number + "\n...Deck: " + globalStore.deck + "\n...Draw Deck: " + drawDeck);
        }
      }
      globalStore.drawDeck = drawDeck;
      return globalStore.drawDeck;
    },
    put: function(cards, where) {
      if(!Array.isArray(cards)) {
        throw "First argument must be an array of cards.";
      }

      switch(where.toLowerCase()) {
        case "top":
          cards.forEach(card => globalStore.deck.unshift(card));
          break;
        case "middle":
          cards.forEach(card => {
            let random = 0;
            do {
              random = Math.floor(Math.random() * globalStore.deck.length);
            } while (random === 0 || (random === globalStore.deck.length - 1));

            globalStore.deck.splice(random, 0, card);
          });
          break;
        case "bottom":
          cards.forEach(card => globalStore.deck.push(card));
          break;
        default:
  //      throw "2nd argument must be 'top', 'middle', or 'bottom'.";
          cards.forEach(card => globalStore.deck.unshift(card));
          break;
      }
      console.log("Deck after new cards is: " + globalStore.deck + "\n");
      return globalStore.deck;
    },
    count: function() {
      console.log("Count is running!")
      if(globalStore.deck.length === 1) {
        console.log("The deck has " + globalStore.deck.length + " card.\n")
      }
      if(globalStore.deck.length === 0) {
        console.log("The deck has no cards, therefore there is no deck!\n")
      }
      console.log("The deck has " + globalStore.deck.length + " cards.\n")
      return globalStore.deck.length;
    }
}

测试是:

let deck = new Deck();
deck.put([1,2,3],"middle")

引发错误,即deck.put不是函数。

对此进行了大量阅读,似乎无法弄清楚如何解决这个问题。任何帮助将不胜感激。

0 个答案:

没有答案