官术网_书友最值得收藏!

Building the getLastBlock method

Now, the next method that we are going to add to our Blockchain constructor function will be the getLastBlock. This method will simply return the last block in our blockchain to us. Follow the below mentioned steps to build the method: 

  1. Go to our dev/blockchain.js file, and after our createNewBlock method, add the following:
Blockchain.prototype.getLastBlock = function () { 

}
  1. Inside of this getLastBlock method, we will type the following highlighted line of code:
Blockchain.prototype.getLastBlock = function () { 
return this.chain[this.chain.length - 1];

}

The [this.chain.length - 1]; in this preceding code defines the position of the block in the chain, which, in our case, is the previous block, therefore negated by 1. This method is simple and straightforward, and we'll use it in later chapters. 

主站蜘蛛池模板: 武山县| 夏河县| 巴塘县| 靖安县| 承德县| 丰县| 衡东县| 高青县| 石狮市| 右玉县| 宁晋县| 资源县| 郑州市| 浑源县| 获嘉县| 贡山| 连州市| 苍梧县| 阿克陶县| 竹山县| 德令哈市| 张家川| 淮安市| 延安市| 孟村| 巴林右旗| 武义县| 阳东县| 兴仁县| 鄂温| 锡林郭勒盟| 甘肃省| 西贡区| 古浪县| 岐山县| 吉隆县| 双柏县| 元谋县| 东明县| 枞阳县| 漳州市|