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

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. 

主站蜘蛛池模板: 温泉县| 乐安县| 华亭县| 昭通市| 怀宁县| 平遥县| 肇庆市| 江津市| 龙江县| 镇原县| 云和县| 安阳县| 大悟县| 栾城县| 台湾省| 临邑县| 沛县| 滦平县| 黑山县| 绩溪县| 通许县| 阿拉善右旗| 哈密市| 天台县| 茶陵县| 蒙城县| 宽甸| 崇义县| 和龙市| 页游| 灵台县| 林西县| 牡丹江市| 濉溪县| 南川市| 民勤县| 天台县| 镇巴县| 南汇区| 柳州市| 互助|