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

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. 

主站蜘蛛池模板: 确山县| 中牟县| 大城县| 介休市| 崇阳县| 花莲市| 平定县| 福清市| 枣阳市| 北海市| 龙门县| 巴塘县| 昌都县| 资讯 | 孝义市| 容城县| 郓城县| 资源县| 金乡县| 崇仁县| 武邑县| 孟连| 嘉义县| 米易县| 鸡东县| 阿克陶县| 来安县| 河西区| 申扎县| 乐至县| 仪征市| 乡宁县| 三原县| 哈尔滨市| 芦山县| 固原市| 郯城县| 华池县| 怀仁县| 洪江市| 施秉县|