create account

Objects in JavaScript By albro by albro

View this thread on: hive.blogpeakd.comecency.com
· @albro ·
$4.37
Objects in JavaScript By albro
<center>![Objects in JavaScript](https://files.peakd.com/file/peakd-hive/albro/23w3CYwmfdeC78acXx68KvPfSyzRBFzYam8wEtcb4vjW8CAAwTsrXUcXCLMtY5T7FoaUf.jpg)</center>

<p>Let's take a new look at JavaScript; It's use! In other words, in a series of posts, after explaining some prerequisites, such as the types of functions and their calls, and the types of objects, I want to access the DOM. That's where the power of JavaScript is shown in practice and I can use the DOM to change the content of a page and its behavior.</p>
<h3>Defining objects in JavaScript</h3>
<p>It can be said that objects in JavaScript are everything in JavaScript! If you understand things, many issues will become easy for you and you will gain a lot of maneuvering power.
</p>
<p>Almost everything in JavaScript is an object:
</p>
<ul>
    <li><code>Boolean</code>s can be objects (using <code>new</code> ).</li>
    <li><code>Number</code>s can be objects (using <code>new</code>).</li>
    <li><code>String</code>s can be objects (using <code>new</code> ).</li>
    <li>
        <code>Date</code> is always an object.
    </li>
    <li>
        <code>Math</code> is always the object.
    </li>
    <li>
        Regular expressions are always objects.
    </li>
    <li>
        <code>Array</code>s are always objects.
    </li>
    <li>
        <code>Function</code>s are always objects.
    </li>
    <li>
        And finally, <code>object</code>s are also of object type.
    </li>
</ul>
<p>In other words, all JavaScript values (except primitive values) are objects.
</p>
<h3>primitive values
</h3>
<p>Primitive values are values that don't have any properties or methods:
</p>
<ul>
    <li>
        <code>string</code>
    </li>
    <li>
        <code>number</code>
    </li>
    <li>
        <code>boolean</code>
    </li>
    <li>
        <code>null</code>
    </li>
    <li>
        <code>undefined</code>
    </li>
</ul>
<p>Primitive values are immutable in JavaScript. You must be saying that we can easily change the numbers, so how are they immutable?
</p>
<p>Let me give you an example; You can change <code>x = 3.14</code> to <code>x = 2</code>. In this case, you have changed the <strong>variable</strong> (<code>x</code>) and not <code>3.14</code>. Another example is that the string <code>"Hello"</code> always remains the same as <code>"Hello"</code> and does not change, but you can give it to a variable or take it from a variable. You cannot change the string itself. JavaScript logic is like this.
</p>
<p><strong>Objects are also a variable type, but the difference is that variables can only hold one value, such as </strong><code>var person = "Hive Blockchain"</code><strong>, but objects can hold many values.</strong>
</p>
<p>These values are stored in the form of <code>name: value</code>:
</p>
<pre><code class="language-javascript">var person = {firstName:"Hive", lastName:"Blockchain", age:3, eyeColor:"red"};</code></pre>
<p>This object can also be written as follows:
</p>
<pre><code class="language-javascript">var person = {
  firstName : "Hive",
  lastName  : "Blockchain",
  age     : 3,
  eyeColor  : "red"
};</code></pre>
<p>We say that JavaScript objects are a collection of named values.
</p>
<h3>properties
</h3>
<p>I said above that values are stored in objects as <code>name: value</code>. We call these names <strong>property</strong>. Therefore, it can be said for the above object:
</p>
<center>
    <table>
        <tbody>
            <tr>
                <td>
                    <center>
                        <strong>Property</strong>
                    </center>
                </td>
                <td>
                    <center>
                        <strong>Value</strong>
                    </center>
                </td>
            </tr>
            <tr>
                <td>
                    <center>
                        firstName
                    </center>
                </td>
                <td>
                    <center>
                        Hive
                    </center>
                </td>
            </tr>
            <tr>
                <td>
                    <center>
                        lastName
                    </center>
                </td>
                <td>
                    <center>
                        Blockchain
                    </center>
                </td>
            </tr>
            <tr>
                <td>
                    <center>
                        age
                    </center>
                </td>
                <td>
                    <center>
                        3
                    </center>
                </td>
            </tr>
            <tr>
                <td>
                    <center>
                        eyeColor
                    </center>
                </td>
                <td>
                    <center>
                        red
                    </center>
                </td>
            </tr>
        </tbody>
    </table>
</center>

<p><strong>Note</strong>: The properties of an object can be primitive values, functions or other objects.
</p>
<h3>methods
</h3>
<p>Methods in JavaScript are actions performed on an object, but the object method is one of the object's properties that has the definition of a function. Pay attention to the following example:
</p>
<center>
    <table>
        <tbody>
            <tr>
                <td>
                    <center>
                        <strong>Property</strong>
                    </center>
                </td>
                <td>
                    <center>
                        <strong>Value</strong>
                    </center>
                </td>
            </tr>
            <tr>
                <td>
                    <center>
                        firstName
                    </center>
                </td>
                <td>
                    <center>
                        Hive
                    </center>
                </td>
            </tr>
            <tr>
                <td>
                    <center>
                        lastName
                    </center>
                </td>
                <td>
                    <center>
                        Blockchain
                    </center>
                </td>
            </tr>
            <tr>
                <td>
                    <center>
                        age
                    </center>
                </td>
                <td>
                    <center>
                        3
                    </center>
                </td>
            </tr>
            <tr>
                <td>
                    <center>
                        eyeColor
                    </center>
                </td>
                <td>
                    <center>
                        red
                    </center>
                </td>
            </tr>
            <tr>
                <td>
                    <center>
                        fullName
                    </center>
                </td>
                <td>
                    <center>
                        function() {return this.firstName + " " + this.lastName;}
                    </center>
                </td>
            </tr>
        </tbody>
    </table>
</center>

<p>So <code>fullName</code> is a method.
</p>
<h3>Create an object
</h3>
<p>There are several ways to create an object in JavaScript:
</p>
<ul>
    <li>Defining an object number manually (object literal)
    </li>
    <li>Define an object number using the <code>new </code>keyword
    </li>
    <li>Defining an object <code>constructor</code> (meaning "object builder" or "object maker") and then building different objects based on that constructor.
    </li>
</ul>
<h3>The first method (object literal)
</h3>
<p>This is the easiest way to create an object. In this method, you put a list of <code>name: values</code> (such as <code>age: 3</code>) inside <code>{}</code> signs. Example:
</p>
<pre><code class="language-javascript"> var person = {firstName:"Hive", lastName:"Blockchain", age:3, eyeColor:"red"};</code></pre>
<p>As I said, white space does not affect the execution, so you can write this object as follows:
</p>
<pre><code class="language-javascript">var person = {
  firstName: "Hive",
  lastName: "Blockchain",
  age: 3,
  eyeColor: "red"
};</code></pre>
<h3>The second method: using <code>new</code>
</h3>
<p>An object is defined simply as follows:
</p>
<pre><code class="language-javascript">var person = new Object();
person.firstName = "Hive";
person.lastName = "Blockchain";
person.age = 3;
person.eyeColor = "red";</code></pre>
<p>This method is the same as the first method. Due to the readability of the code and its faster execution, I suggest you never use this method and choose the first method instead.
</p>
<h3>A common mistake about objects
</h3>
<p>If we have an object called <code>person</code>, the following code cannot copy it:
</p>
<pre><code class="language-javascript">var x = person;</code></pre>
<p>In fact, variable <code>x</code> is not a copy of <code>person</code>, but it is itself, and both are the same. Any changes you make in <code>x</code> will also be made in <code>person</code>. So:
</p>
<pre><code class="language-javascript">var person = {firstName:"Hive", lastName:"Blockchain", age:3, eyeColor:"red"}
var x = person;
x.age = 4;</code></pre>
<p>This code (i.e. <code>x.age = 10</code>) changes both <code>x</code> and <code>person</code> because both are the same object.
</p>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 339 others
properties (23)
authoralbro
permlinkobjects-in-javascript-by-albro
categoryhive-169321
json_metadata{"app":"peakd/2023.11.3","format":"markdown","author":"albro","tags":["development","programming","posh","gosh","threads","hive-engine","neoxian","lassecash","tricks","leofinance"],"users":[],"image":["https://files.peakd.com/file/peakd-hive/albro/23w3CYwmfdeC78acXx68KvPfSyzRBFzYam8wEtcb4vjW8CAAwTsrXUcXCLMtY5T7FoaUf.jpg"]}
created2023-12-01 21:30:00
last_update2023-12-01 21:30:00
depth0
children5
last_payout2023-12-08 21:30:00
cashout_time1969-12-31 23:59:59
total_payout_value2.198 HBD
curator_payout_value2.171 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9,564
author_reputation30,477,419,385,789
root_title"Objects in JavaScript By albro"
beneficiaries
0.
accounthive-169321
weight200
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,333,987
net_rshares9,224,797,783,462
author_curate_reward""
vote details (403)
@chessbrotherspro ·
<h3>Congratulations!</h3><hr /><div class="pull-right"><img src="https://images.hive.blog/DQmQLssYuuJP2neoTVUbMRzvAu4Ptg7Vwt92aTM7Z3gNovg/cb-logo-150.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><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></p></div><div class="text-center"><p><br>Kindly</p><p><strong><em>The CHESS BROTHERS team</em></strong></p></div>
👍  
properties (23)
authorchessbrotherspro
permlinkre-objects-in-javascript-by-albro
categoryhive-169321
json_metadata""
created2023-12-02 12:34:00
last_update2023-12-02 12:34:00
depth1
children0
last_payout2023-12-09 12:34:00
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,598
author_reputation78,000,753,163,525
root_title"Objects in JavaScript By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,349,181
net_rshares3,541,407,395
author_curate_reward""
vote details (1)
@kushyzee ·
I haven't learned JavaScript yet but I could still understand all that you have said and that's because objects in JavaScript are a lot similar to dictionaries in python. The same curly brackets {} and property-value pairs (but in python we just call them key-value pairs) 
👍  
properties (23)
authorkushyzee
permlinkre-albro-2023122t233111721z
categoryhive-169321
json_metadata{"type":"comment","tags":["hive-169321","development","programming","posh","gosh","threads","hive-engine","neoxian","lassecash","tricks","leofinance"],"app":"ecency/3.0.44-mobile","format":"markdown+html"}
created2023-12-02 22:31:12
last_update2023-12-02 22:31:12
depth1
children1
last_payout2023-12-09 22:31: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_length273
author_reputation91,879,023,731,471
root_title"Objects in JavaScript By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,360,624
net_rshares3,544,177,907
author_curate_reward""
vote details (1)
@albro ·
I've always said this and I'll repeat it again: regardless of the different syntax, the principles are always the same. You should always learn the basics.
properties (22)
authoralbro
permlinkre-kushyzee-2023123t13814756z
categoryhive-169321
json_metadata{"tags":["hive-169321","development","programming","posh","gosh","threads","hive-engine","neoxian","lassecash","tricks","leofinance"],"app":"ecency/3.0.37-vision","format":"markdown+html"}
created2023-12-03 09:38:15
last_update2023-12-03 09:38:15
depth2
children0
last_payout2023-12-10 09:38: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_length155
author_reputation30,477,419,385,789
root_title"Objects in JavaScript By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,369,797
net_rshares0
@poshthreads ·
https://inleo.io/threads/albro/re-leothreads-cgkruzvv
<sub> The rewards earned on this comment will go directly to the people ( albro ) sharing the post on LeoThreads,LikeTu,dBuzz.</sub>
properties (22)
authorposhthreads
permlinkre-albro-objects-in-javascript-by-albro-1719
categoryhive-169321
json_metadata"{"app":"Poshtoken 0.0.2","payoutToUser":["albro"]}"
created2023-12-02 11:21:15
last_update2023-12-02 11:21:15
depth1
children0
last_payout2023-12-09 11:21: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_length187
author_reputation415,471,585,053,248
root_title"Objects in JavaScript By albro"
beneficiaries
0.
accountnomnomnomnom
weight10,000
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id129,348,008
net_rshares0
@stemsocial ·
re-albro-objects-in-javascript-by-albro-20231203t002611867z
<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-objects-in-javascript-by-albro-20231203t002611867z
categoryhive-169321
json_metadata{"app":"STEMsocial"}
created2023-12-03 00:26:12
last_update2023-12-03 00:26:12
depth1
children0
last_payout2023-12-10 00:26: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_length565
author_reputation22,918,491,691,707
root_title"Objects in JavaScript By albro"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id129,362,755
net_rshares3,469,604,206
author_curate_reward""
vote details (1)