Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn Blockchain Programming with JavaScript

You're reading from   Learn Blockchain Programming with JavaScript Build your very own Blockchain and decentralized network with JavaScript and Node.js

Arrow left icon
Product type Paperback
Published in Nov 2018
Publisher Packt
ISBN-13 9781789618822
Length 252 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Eric Traub Eric Traub
Author Profile Icon Eric Traub
Eric Traub
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Setting up the Project 2. Building a Blockchain FREE CHAPTER 3. Accessing the Blockchain through an API 4. Creating a Decentralized Blockchain Network 5. Synchronizing the Network 6. Consensus Algorithms 7. Block Explorer 8. In conclusion... 9. Other Books You May Enjoy

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.

...
You have been reading a chapter from
Learn Blockchain Programming with JavaScript
Published in: Nov 2018
Publisher: Packt
ISBN-13: 9781789618822
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime