无法运行JavaScript代码

时间:2018-07-21 16:26:00

标签: javascript blockchain

    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/sha256.js"></script>
<script type="text/javascript">

    class Block{
        calculateHash(){
            var cipher = CryptoJS.SHA256(this.email + this.username + this.password + this.fullname + this.telnum + this.icnum + this.previousHash);
            return cipher;
        }

        constructor(email, username, password, fullname, telnum, icnum, previousHash){
            this.email = email;
            this.username = username;
            this.password = password;
            this.fullname = fullname;
            this.telnum = telnum;
            this.icnum = icnum;
            this.previousHash = previousHash;
            this.hash = calculateHash();
        }



    }

    class Blockchain{
        constructor(){
            this.chain = [this.createGenesisBlock()];
        }

        createGenesisBlock(){
            return new Block("example@gmail.com", "genesisblock", "example1234", "examplename", "0123456789", "860914-01-6767", "0");
        }

        getLatestBlock(){
            return this.chain[this.chain.length - 1];
        }

        addBlock(newBlock){
            newBlock.previousHash = this.getLatestBlock().hash;
            newBlock.hash = newBlock.calculateHash;
            this.chain.push(newBlock);
        }
    }



    let Mcoin = new Blockchain();
    Mcoin.addBlock(new Block("example@gmail.com", "genesisblock", "example1234", "examplename", "0123456789", "860914-01-6767"));

    console.log(JSON.stringify(Mcoin, null, 4));
</script>

我写了一个在youtube上找到的简单的javascript区块链代码,我不明白为什么我不能运行它。我在php文件上执行了此代码,因为我需要引用链接以将sha256函数用于我的哈希。在最后一行,我可以在控制台上查看链条,但是当我进入控制台时,它仅显示此内容。

  

未捕获的ReferenceError:未在Blockchain的新Block(test_code.php:18)处定义calculateHash。在test_code.php:47处的新Blockchain(test_code.php:27)处未创建genesisBlock(test_code.php:31)

youtube上的那个家伙设法在他的控制台上查看了他的链,但是我做了完全一样的事情,但是什么也没出现。我做错了calculateHash()函数吗?

1 个答案:

答案 0 :(得分:1)

this.hash = calculateHash();更改为this.hash = this.calculateHash();

正如Xufox在评论中所说:calculateHash不是该作用域中存在的功能;这是一个实例方法,可以作为new Block().calculateHashthis.calculateHash使用。

<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/sha256.js"></script>
<script type="text/javascript">

    class Block{
        calculateHash(){
            var cipher = CryptoJS.SHA256(this.email + this.username + this.password + this.fullname + this.telnum + this.icnum + this.previousHash);
            return cipher;
        }

        constructor(email, username, password, fullname, telnum, icnum, previousHash){
            this.email = email;
            this.username = username;
            this.password = password;
            this.fullname = fullname;
            this.telnum = telnum;
            this.icnum = icnum;
            this.previousHash = previousHash;
            this.hash = this.calculateHash();
        }



    }

    class Blockchain{
        constructor(){
            this.chain = [this.createGenesisBlock()];
        }

        createGenesisBlock(){
            return new Block("example@gmail.com", "genesisblock", "example1234", "examplename", "0123456789", "860914-01-6767", "0");
        }

        getLatestBlock(){
            return this.chain[this.chain.length - 1];
        }

        addBlock(newBlock){
            newBlock.previousHash = this.getLatestBlock().hash;
            newBlock.hash = newBlock.calculateHash;
            this.chain.push(newBlock);
        }
    }



    let Mcoin = new Blockchain();
    Mcoin.addBlock(new Block("example@gmail.com", "genesisblock", "example1234", "examplename", "0123456789", "860914-01-6767"));

    console.log(JSON.stringify(Mcoin, null, 4));
</script>

JSFiddle:http://jsfiddle.net/8aktL5zp/1/