- Learn Blockchain Programming with JavaScript
- Eric Traub
- 794字
- 2021-06-10 18:40:35
Explanation of the prototype object
Another important concept that we need to discuss before getting into coding our blockchain data structure is the prototype object. The prototype object is simply an object that multiple other objects can refer to in order to get any information or functionality that they need. For our example, which we discussed in the previous section, each of our constructor functions will have a prototype that all of their instances will be able to refer to. Let's try to understand what a prototype object means by exploring a couple of examples.
For example, if we take our User constructor function that we created in the previous section, we can put those properties onto its prototype. Then, all of our user instances like user1 and user200 will have access to and be able to use that prototype. Let's add a property on our User prototype and see what happens. To add a property on the user prototype, we will type the following code:
User.prototype.
Then let's add the name of the property to the preceding code. For example, let's say we want a property email domain:
User.prototype.emailDomain
For our example, assume that Facebook wants every user to have an @facebook.com email address, so we'll set the email domain property as follows:
User.prototype.emailDomain = '@facebook.com';
Now let's check out our user1 object again:
In the preceding screenshot, we can see that user1 does not have the email domain property that we just added to it. However, we can expand the user1 object, as well as its dunder proto, as highlighted in the following screenshot:
When we do this, we can observe the emailDomain property that we just added, which is set to @facebook.com.
Just to clarify, the dunder proto and the prototype object that we actually put the emailDomain property on are actually not exactly the same, but are very similar. Basically, anything that we put on the constructor function prototype will have access to the dunder proto of any of the objects that we create with the constructor function.
So, if we put emailDomain on the constructor function prototype, we'll have access to it on the user1 dunder proto, the user200 dunder proto, and the dunder protos of any other user instance that we've created.
Now let's get back to the emailDomain property. We put the emailDomain property and the user prototype. We can see that we don't have the property on the actual user200 object, but we have that property under the user200 dunder proto. So, if we type the following command, we will still have access to that property:
user200.emailDomain
We should then see the following output:
So, this is how the prototype object works. If we put a property on the constructor function's prototype, all of the instances of the constructor function will have access to that property.
The same thing applies for any methods or functions that we might want all of our instances to have. Let's take a look at another example, assuming that we want all of our user instances to have a getEmailAddress method. We can put this on the prototype of the constructor function as follows:
User.prototype.getEmailAddress = function () {
}
Now let's have this getEmailAddress method return some specific properties, as follows (highlighted):
User.prototype.getEmailAddress = function () {
return this.firstName + this.lastName + this.emailDomain;
}
Now both user1 and user200 should have this method under their dunder proto, so let's check it out. Type in our users, and under their dunder proto you will get to observe the preceding function, as shown in the following screenshot:
In the preceding screenshot, we can observe that both user1 and user200 have the getEmailAddress method under their dunder proto.
Now, if we type user200.getEmailAddress and then invoke it, the method will then create user200's Facebook email address for us, as shown in the following screenshot:
A similar thing will happen if we invoke the method for user1:
So, this is how we use the prototype object with a constructor function. If we want our constructor function instances to all have the properties that are the same for all of them, or all have a method that is the same for all of them, we will put it on the prototype instead of the constructor function itself. This will help in keeping the instances more lean and cleaner.
This is all the background information that we need to know in order to start coding our blockchain data structure. In the following section, we will start building our blockchain by using a constructor function and the prototype object.
- 自己動手寫搜索引擎
- Hands-On Machine Learning with scikit:learn and Scientific Python Toolkits
- Building a RESTful Web Service with Spring
- Rake Task Management Essentials
- Mastering Kotlin
- INSTANT CakePHP Starter
- 人臉識別原理及算法:動態人臉識別系統研究
- Learning ArcGIS Pro
- 深度強化學習算法與實踐:基于PyTorch的實現
- Drupal 8 Module Development
- Java EE 8 Application Development
- 全棧自動化測試實戰:基于TestNG、HttpClient、Selenium和Appium
- Fast Data Processing with Spark(Second Edition)
- 大學計算機基礎實驗指導
- jQuery for Designers Beginner's Guide Second Edition