create account

javascript prototypes, ES5 & Tricks By albro by albro

View this thread on: hive.blogpeakd.comecency.com
· @albro ·
$6.56
javascript prototypes, ES5 & Tricks By albro
<center>![javascript prototypes & ES5](https://files.peakd.com/file/peakd-hive/albro/23viR9N3TuF2GVayLi3syUEP13UKJxtKrc2qfnhtXyeKTMiXe8cSorM4yHZpA4eog6M9A.png)</center>

<p>
    In this post, I'll talk about a concept that may be overlooked by most beginner and sometimes professional programmers: prototypes. Then I'll introduce some methods in ECMAScript 5 because they're very useful.
</p>
<h2>
    What is a prototype?
</h2>
<p>
    In fact, all JavaScript objects inherit their methods and properties from a prototype. These prototypes, as their name suggests, are prototypes and outlines for a class of objects.
</p>
<p>
    In the <a href="https://ecency.com/hive-169321/@albro/constructor-in-javascript-by-albro">previous session</a>, I talked about object constructors and gave you the following example:
</p>
<pre><code class="language-javascript">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;h2&gt;JavaScript Objects&lt;/h2&gt;
&lt;p id="demo"&gt;&lt;/p&gt;
&lt;script&gt;
function Person(username, rc, age, eye) {
  this.username = username;
  this.rc = rc;
  this.age = age;
  this.eyeColor = eye;
}
var myFriends = new Person("ocdb", 100, 5, "blue");
var myLove = new Person("leo.voter",100,48,"green");
document.getElementById("demo").innerHTML =
"My Friend is " + myFriends.age + ". My love is " + myLove.age; 
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>

<center>![Js Objects](https://files.peakd.com/file/peakd-hive/albro/23u6YysF9NMXiARSdP3TsxGGwxryR5Wm1AGbWB9SWQckr2D14WiYxNEcJy2CfEjvK3g64.png)</center>

<p>
    We cannot add our desired properties to the constructor, and the following code was incorrect:
</p>
<pre><code class="language-javascript">Person.nationality = "English";</code></pre>
<p>
    Rather, we should have added it manually to the constructor function itself:
</p>
<pre><code class="language-javascript">function Person(username, rc, age, eye) {
  this.username = username;
  this.rc = rc;
  this.age = age;
  this.eyeColor = eye;
  this.nationality = "English";
}</code></pre>
<h3>
    Inherit from prototype
</h3>
<p>
    All JavaScript objects inherit their methods and properties from a prototype, but we did not specify which prototype:
</p>
<ul style="list-style-type:disc;">
    <li>
        <code>Dates</code> inherit from <code>Date.prototype</code>.
    </li>
    <li>
        <code>Arrays</code> inherit from <code>Array.prototype</code>.
    </li>
    <li>
        <code>Persons</code> (the same as our example) inherit from <code>Person.prototype</code>.
    </li>
</ul>
<p>
    So the general rule is that an <code>object</code> inherits from <code>Object.prototype</code>.
</p>
<h3>
    Examples of using prototype
</h3>
<p>
    Now that we have learned about the prototype, you should know that we can access the constructor function through it.
</p>
<p>
    In this example, I want to use prototype to create a new property that did not exist in our object.
</p>
<pre><code class="language-javascript">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;h2&gt;JavaScript Objects&lt;/h2&gt;
&lt;p id="demo"&gt;&lt;/p&gt;
&lt;script&gt;
function Person(username, rc, age, eye) {
  this.username = username;
  this.rc = rc;
  this.age = age;
  this.eyeColor = eye;
}
Person.prototype.nationality = "English";
var myFriends = new Person("ocdb", 100, 5, "blue");
document.getElementById("demo").innerHTML =
"The nationality of my friend is " + myFriends.nationality; 
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>

<center>![Js prototype.png](https://files.peakd.com/file/peakd-hive/albro/23uRLPLApFg7LnmXo4AZcGV2wDmBRBk6LxfguoLLsgQhf4csqdn9baeQWZS3Lgj26Cvr3.png)</center>

<p>
    As you can see, I easily added the <code>nationality</code> attribute.
</p>
<p>
    I do the same to add methods:
</p>
<pre><code class="language-javascript">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
&lt;h2&gt;JavaScript Objects&lt;/h2&gt;
&lt;p id="demo"&gt;&lt;/p&gt;
&lt;script&gt;
function Person(username, rc, age, eye) {
  this.username = username;
  this.rc = rc;
  this.age = age;
  this.eyeColor = eye;
}
Person.prototype.details = function() {
  return this.username + " RC = " + this.rc
};
var myFriends = new Person("ocdb", 100, 5, "blue");
document.getElementById("demo").innerHTML =
"The details of my friend is " + myFriends.details(); 
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>

<center>![Js Prototype Methods](https://files.peakd.com/file/peakd-hive/albro/23u6Z4abZkgLkvLYt8dQ9aN38khJTnxwTj6RefrGxsbVrQGdQcPeBaj2KXv5EeRYnEe36.png)</center>

<p>
    <strong>Warning:</strong> <i>only change your own prototypes! Do not touch JavaScript's default prototypes and its standard objects, because the whole program will be messed up.</i>
</p>
<h3>
    ECMAScript 5 methods
</h3>
<p>
    In ECMAScript 5, methods for working with objects were introduced. I want to say the most important ones here:
</p>
<pre><code class="language-javascript">// Add or change a property of an object
Object.defineProperty(object, property, descriptor)
// Add or change several properties
Object.defineProperties(object, descriptors)
// Accessing properties
Object.getOwnPropertyDescriptor(object, property)
// Return all properties as an array
Object.getOwnPropertyNames(object)
// Return numeric properties in the form of an array
Object.keys(object)
// access the prototype
Object.getPrototypeOf(object)
// Denying permission to add properties to an object
Object.preventExtensions(object)
// Returns true if we are allowed to add properties to an object
Object.isExtensible(object)
// Denies the permission to change the properties themselves (and not the values).
Object.seal(object)
// Returns true if we are not allowed to add properties to an object
Object.isSealed(object)
// Prevents any changes to the object
Object.freeze(object)
// Returns true if we are not allowed to make changes to the value of the object
Object.isFrozen(object)</code></pre>
<h3>
    Changing the value of a property
</h3>
<p>
    The general structure of the code is as follows:
</p>
<pre><code class="language-javascript">Object.defineProperty(object, property, {value : value})</code></pre>
<p>
    Example:
</p>
<pre><code class="language-javascript">var person = {
  username : "leo.voter",
  rc : "90",
  language : "EN" 
};
// Change a property
Object.defineProperty(person, "language", {value : "NO"});</code></pre>
<h3>
    Change metadata
</h3>
<p>
    The metadata change is as follows:
</p>
<pre><code class="language-javascript">writable : true      // The value of the properties can be changed
enumerable : true    // Properties are countable
configurable : true  // Properties can be configured and adjusted.</code></pre>
<p>
    We also have a False value for each of them, which will be the opposite of the above explanation.
</p>
<h3>
    Get all the properties
</h3>
<p>
    To get all the properties of an object, I can do this:
</p>
<pre><code class="language-javascript">var person = {
  username : "leo.voter",
  rc : "90",
  language : "EN" 
};
Object.defineProperty(person, "language", {enumerable:false});
Object.getOwnPropertyNames(person);  // Returns an array of all properties</code></pre>
<h3>
    Definition of Getter and Setter
</h3>
<p>
    In the previous parts, I talked about <code>Getter</code> and <code>Setter</code>, but you should know that you can also define them with <code>Object.defineProperty()</code>:
</p>
<pre><code class="language-javascript">// Create an object
var person = {
  username : "leo.voter",
  rc : "90",
  language : "EN" 
};
// Define a getter
Object.defineProperty(person, "details", {
  get : function () {return this.username + " RC = " + this.rc;}
});</code></pre>
<p>
    I hope you enjoyed this post. If you liked this post, don't forget to upvote!
</p>
<p>
    &nbsp;
</p>
<p>
    [Hive: @albro]
</p>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 460 others
properties (23)
authoralbro
permlinkjavascript-prototypes-es5-and-tricks-by-albro
categoryhive-169321
json_metadata"{"app":"peakd/2023.11.3","format":"markdown","description":"In this post, I'll talk about a concept that may be overlooked by most programmers: prototypes and some methods in ES5.","tags":["programming","development","gosh","threads","neoxian","tricks","hive-engine","chessbrothers","stem","leofinance"],"users":["albro"],"image":["https://files.peakd.com/file/peakd-hive/albro/23viR9N3TuF2GVayLi3syUEP13UKJxtKrc2qfnhtXyeKTMiXe8cSorM4yHZpA4eog6M9A.png","https://files.peakd.com/file/peakd-hive/albro/23u6YysF9NMXiARSdP3TsxGGwxryR5Wm1AGbWB9SWQckr2D14WiYxNEcJy2CfEjvK3g64.png","https://files.peakd.com/file/peakd-hive/albro/23uRLPLApFg7LnmXo4AZcGV2wDmBRBk6LxfguoLLsgQhf4csqdn9baeQWZS3Lgj26Cvr3.png","https://files.peakd.com/file/peakd-hive/albro/23u6Z4abZkgLkvLYt8dQ9aN38khJTnxwTj6RefrGxsbVrQGdQcPeBaj2KXv5EeRYnEe36.png"]}"
created2023-12-22 19:33:00
last_update2023-12-22 19:33:00
depth0
children4
last_payout2023-12-29 19:33:00
cashout_time1969-12-31 23:59:59
total_payout_value3.293 HBD
curator_payout_value3.271 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,839
author_reputation30,477,419,385,789
root_title"javascript prototypes, ES5 & Tricks By albro"
beneficiaries
0.
accounthive-169321
weight200
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,907,039
net_rshares13,772,315,075,233
author_curate_reward""
vote details (524)
@chessbrotherspro ·
<h3>Congratulations!</h3><hr /><div class="pull-right"><img src="https://files.peakd.com/file/peakd-hive/chessbrotherspro/AJoJKGVARKHFCTHG7ee3GNkn5RMN7wixeJ52ipAgzDZ4QmeTcBdsk8hpi4pgj4e.png" alt="You have obtained a vote from CHESS BROTHERS PROJECT"/></div><div class="text-justify"><h3>✅ Good job. Your post has been appreciated and has received support from <a href="/@chessbrotherspro"><b>CHESS BROTHERS</b></a> ♔ 💪</h3><p><br>♟ We invite you to use our hashtag <b>#chessbrothers</b> and learn more <a href="/@chessbrotherspro/introducing-chess-brothers-project-the-most-innovative-community-combining-chess-fitness-and-more"><b>about us</b></a>.</p><p>♟♟ You can also reach us on our <a href="https://discord.gg/73sK9ZTGqJ" rel="noopener" title="This is going to take you to the Discord of Chess Brothers"><b>Discord server</b></a>  and promote your posts there. </p><p>♟♟♟  Consider <a href="/@chessbrotherspro/teamwork-is-worthwhile-join-the-chess-brothers-healing-trail-supporting-the-work-being-done-and-earning-rewards"><b>joining our curation trail</b></a> so we work as a team and you get rewards automatically.</p><p>♞♟ Check out our <a href="/@chessbrotherspro"><b>@chessbrotherspro</b></a> account to learn about the curation process carried out daily by our team.</p><br>🥇 If you want to earn profits with your HP delegation and support our project, we invite you to join the <i>Master Investor</i> plan. <a href="/@chessbrotherspro/master-investor-plan-or-programa">Here you can learn how to do it.</a></div><div class="text-center"><p><br>Kindly</p><p><strong><em>The CHESS BROTHERS team</em></strong></p></div>
👍  
properties (23)
authorchessbrotherspro
permlinkre-javascript-prototypes-es5-and-tricks-by-albro-20231223t065111z
categoryhive-169321
json_metadata"{"app": "beem/0.24.26"}"
created2023-12-23 06:51:12
last_update2023-12-23 06:51:12
depth1
children0
last_payout2023-12-30 06:51:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,628
author_reputation78,000,503,134,261
root_title"javascript prototypes, ES5 & Tricks By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,919,857
net_rshares2,741,936,051
author_curate_reward""
vote details (1)
@gwajnberg ·
That type of script is interesting! Examples using the blockchain always amaze me!

`<center>https://files.peakd.com/file/peakd-hive/dlmmqb/23tkn1F4Yd2BhWigkZ46jQdMmkDRKagirLr5Gh4iMq9TNBiS7anhAE71y9JqRuy1j77qS.png</center>

<center> <b>
Want to Know more about Hivepakistan? 

Ping Us On [Hive Pakistan Discord server](https://discord.gg/3FzxCqFYyG)

To support HivePakistan, delegate Hive Power to hivepakistan and earn 90% curation reward :) 

Here are some handy links for delegation

</b>
</center>

<center>| [50 HP](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=hivepakistan&vesting_shares=50%20HP) | [100 HP](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=hivepakistan&vesting_shares=100%20HP) |[500 HP](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=hivepakistan&vesting_shares=500%20HP) | [1000 HP](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=hivepakistan&vesting_shares=1000%20HP) | [5000 HP](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=hivepakistan&vesting_shares=5000%20HP) | [10000 HP](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=hivepakistan&vesting_shares=10000%20HP) | [20000 HP](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=hivepakistan&vesting_shares=20000%20HP) |

A delegation of 500 or more HP makes you earn Hivepakistan supporter badge.

</center>
`
👍  
properties (23)
authorgwajnberg
permlinkre-albro-20231222t16144613z
categoryhive-169321
json_metadata{"image":["https://files.peakd.com/file/peakd-hive/dlmmqb/23tkn1F4Yd2BhWigkZ46jQdMmkDRKagirLr5Gh4iMq9TNBiS7anhAE71y9JqRuy1j77qS.png"],"image_ratios":[4.8],"type":"comment","tags":["hive-169321","programming","development","gosh","threads","neoxian","tricks","hive-engine","chessbrothers","stem","leofinance"],"app":"ecency/3.0.44-mobile","format":"markdown+html"}
created2023-12-22 23:14:45
last_update2023-12-22 23:14:45
depth1
children0
last_payout2023-12-29 23:14:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,452
author_reputation371,061,746,426,989
root_title"javascript prototypes, ES5 & Tricks By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,913,287
net_rshares2,722,933,824
author_curate_reward""
vote details (1)
@hivebuzz ·
Congratulations @albro! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@albro/replies.png?202312230736"></td><td>You got more than 500 replies.<br>Your next target is to reach 600 replies.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@albro) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Check out our last posts:**
<table><tr><td><a href="/hivebuzz/@hivebuzz/christmas-2023"><img src="https://images.hive.blog/64x128/https://files.peakd.com/file/peakd-hive/hivebuzz/48PxLszsgqS1gaev6FeBYEsrt9sKNhKtddSS32hKnvM3Xam6KCMs3rza5BPR7AQNaF.png"></a></td><td><a href="/hivebuzz/@hivebuzz/christmas-2023">It's the Christmas season: give your friends a gift</a></td></tr></table>
👍  
properties (23)
authorhivebuzz
permlinknotify-albro-20231223t073824
categoryhive-169321
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2023-12-23 07:38:24
last_update2023-12-23 07:38:24
depth1
children0
last_payout2023-12-30 07:38:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length997
author_reputation369,399,718,796,281
root_title"javascript prototypes, ES5 & Tricks By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,920,418
net_rshares2,704,080,581
author_curate_reward""
vote details (1)
@stemsocial ·
re-albro-javascript-prototypes-es5-and-tricks-by-albro-20231223t081315112z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.&nbsp;<br />&nbsp;<br />
</div>
👍  
properties (23)
authorstemsocial
permlinkre-albro-javascript-prototypes-es5-and-tricks-by-albro-20231223t081315112z
categoryhive-169321
json_metadata{"app":"STEMsocial"}
created2023-12-23 08:13:15
last_update2023-12-23 08:13:15
depth1
children0
last_payout2023-12-30 08:13:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation22,918,491,691,707
root_title"javascript prototypes, ES5 & Tricks By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,920,817
net_rshares7,971,628,459
author_curate_reward""
vote details (1)