Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Token, not defined. OOP JavaScript 4 in a row.

I'm using the exact same code files as the video but I'm getting a "Token not defined" error. I tried exporting the Token class with export default Token then importing it with import Token from 'path/to/Token.js'; at the top but then I get an caught reference error.

Please any help is appreciated as this is frustrating. I'm using cloud9 workspace. JavaScript

import Token from 'Token.js';
class Player {
    constructor(name, id, color, active = false){
        this.name = name;
        this.id = id;
        this.color = color;
        this.active = active;
        this.tokens = this.createTokens(21);
    }

    /**
      * Creates token objects for player
      * @param   {integer}   num - Number of token objects to be created
      * @return  {array}     tokens - an array of new token objects
    */
    createTokens(num){
        const tokens = [];

        for(let i = 0; i <num; i++){
            let token = new Token(i, this);
            tokens.push(token);
        }

        return tokens;
    }

    get unusedTokens(){
        return this.tokens.filter(token => !token.dropped);
    }

    get activeToken(){
        return this.unusedTokens[0];
    }
}

/*global $*/
class Token {
    constructor(owner, index){
        this.owner = owner;
        this.id = `token-${index}-${owner.id}`;
        this.dropped = false;
        this.columnLocation  = 0;
    }

    drawHTMLToken(){
        const div = document.createElement('div');
        document.getElementById('get-board-underlay').appendChild(div);
        div.setAttribute('id', this.id);
        div.setAttribute('class', 'token');
        div.style.backgroundColor = this.owner.color;

    }

    get HTMLToken(){
        return this.token;
    }

    get offsetLeft(){
        return this.htmlToken.offsetLeft;
    }

    /**
    *Moves Token one column to left
    *
    */
    moveLeft(){
        if(this.columnLocation > 0){
            this.htmlToken.style.left = this.offsetLeft - 76;
            this.columnLocation -= 1;
        }
    }

    /**
    *Moves Token one column to right
    *
    */
    moveRight(col){
        if(this.columnLocation < col -1){
            this.htmlToken.style.left = this.offsetLeft + 76;
            this.columnLocation += 1;
        }
    }

    /** 
     * Drops html token into targeted board space.
     * @param   {Object}   target - Targeted space for dropped token.
     * @param   {function} reset  - The reset function to call after the drop animation has completed.
     */
    drop(target, reset){
        this.dropped = true;
        $(this.htmlToken).animate({
            top: (target.y * target.diameter)
            }, 750, 'easeOutBounce', reset);
        }
}
export default Token

1 Answer

In the project files the parameters for the Token constructor are reversed.

class Token {
    constructor(index, owner){
        this.owner = owner;
        this.id = `token-${index}-${owner.id}`;

Important difference when creating tokens as in the code here:

createTokens(num){
        const tokens = [];

        for(let i = 0; i <num; i++){
            let token = new Token(i, this);
            tokens.push(token); 

KRIS NIKOLAISEN sorry, I don't think this is helping.