0

I have one file which is block.js:

class Block{
    constructor(timeStamp, lastBlockHash, thisBlockData, thisBlockHash){
        this.timeStamp = timeStamp;
        this.lastBlockHash = lastBlockHash;
        this.thisBlockData = thisBlockData; 
        this.thisBlockHash = thisBlockHash;
        }

    static genesis(){
        return new this(Date.now(), "---", "genesis block", "hash of the genesis");
    }
}

I have another file blockchain.js where I have the following:

const Block = require('./block');
class BlockChain{
    constructor() {
       this.chain = BlockInstance.genesis();
    }
}

and i have a test file where I am doing:

const Block = require("./block.js");
const BlockChain = require("./blockchain.js");
console.log(BlockChain.chain);

I get a "undefined" object in the print output.. this is really driving me nuts as i have already spent more than 4 hours on it.. if anyone can solve this mystery for me then a round of beers on me..

Cheers, alchemist

3
  • 2
    I think you have to still instantiate the classes and not just require them. Commented Sep 18, 2018 at 12:11
  • 4
    You don't create any instance in your code. You need to use the new keyword for creating a BlockChain instance. The class itself doesn't have chain property, it's instances have! Commented Sep 18, 2018 at 12:11
  • you guys are right.. thanks a million.. :)) Commented Sep 18, 2018 at 12:13

2 Answers 2

1

You should instantiate the classes like

const Block = require("./block.js");
const BlockChain = require("./blockchain.js");
let block = new Block(...);
let blockChain = new BlockChain();
console.log(blockChain.chain);

For an example of building a blockchain with JS, you may want to follow a site like This one from medium

The genesis() method would be more suited to be a part of the chain since that is a property of a chain not a different type of block.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Daniel.. it is somehow still not working.. :/ this is my dev-test.js script so far: const Block = require("./block.js"); const BlockChain= require("./blockchain.js"); let blockInstance = new Block(); let blockChainInstance = new BlockChain(); console.log(blockInstance.mineBlock(blockInstance.Genesis(), "First Block Afer Geneis")); console.log(blockChainInstance.chain);
thanks for replying.. lets say i am writing a test case to just test blockchain.js.. and i forget completely about block.js.. since when i instantiate blockchaininstance the constructor inside it with this code: this.chain= Block.genesis() should create a genesis block..
great.. thanks a million :)) on a final note.. Block.genesis() cannot be called without instantiating the Block class directly? because if i do console.log with Block.genesis() it works.. because inside genesis function i have " return new this()" which instantiates block...
That should be done if it was a static method, but I would strongly encourage you to move the genesis() to the BlockChain class over the Block class.
0

The problem is syntactical binding. You need to instantiate your BlockChain class like this:

const myBlockChain = new BlockChain();
console.log(myBlockChain.chain);

Check out MDN for more info on this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.