<div class="container-fluid">
<div class="row">
<div class="col-sm-12">
<h1> OMG! Objects<h1>
<img src="https://i.etsystatic.com/17743589/r/il/874c66/1799726184/il_794xN.1799726184_1plh.jpg" style="width: 250px;">
</div> <!-- end col-sm-12 -->
</div> <!-- end of row -->
</div><!-- end of container-fluid -->
<div class="container-fluid">
<div class="row">
<div class="col-sm-6">
<h1>What are objects</h1>
<p>Objects are a data type. JavaScript follows an 'object based paradigm'. Which means in JS an object is a stand-alone entity (object) with properties. Properties come in key: value pairs. They are like variables. In the sense that variables reference some value in memory. The same way, in objects the property key: value pairs are akin to variables.</p>
</div><!-- end of col-sm-6 -->
<div class="col-sm-6">
<h1>Why?</h1>
<p>Think about it... In the real world most things are "objects". For example your car, the food you eat, the bed you sleep on etc. So the objects in your real life have <span class="special">properties</span> (example red car, thin mattress) and there is some <span class="special">functionality</span> associated with the objects. The car has "driving" functionality and the "mattress" has the "sleep" functionality.</p>
<p>Isn't the whole point of JS to mimic real life stuff (objects) so that you can make apps example <b>Uber</b></p>
</div><!-- end of col-sm-6 -->
</div> <!-- end of row -->
</div><!-- end of container-fluid -->
<div class="container-fluid">
<div class="row">
<div class="col-sm-6">
<h1>Properties</h1>
<p>Just like real life objects have characteristics example a "red" car. The same way in JavaScript we can create objects that have certain characteristics. Except in JS these are called "properties" and not characteristics.</p>
<p>Objects have properties. A property is a "key" : "value" pair. This association may be familiar to you from attributes and values in HTML. Example a div class="myDiv". The values can be of any data type example strings, numbers, booleans, an array.</p>
<p> Example:</p>
<p> name: "Rocko"</p>
</div> <!-- end of col-sm-6 -->
<div class="col-sm-6">
<h1>Method</h1>
<p>A method refers to a function of an object. It is a the value of a property in an object. Think of a method as an "action" of an object. Example the method/action of a car object is driving.</p>
<p>Example:</p>
like: function(){
console.log("I love treaties!")
}
</div> <!-- end of col-sm-6 -->
</div> <!-- end of row -->
</div><!-- end of of container -->
<div class="container-fluid">
<h1>How?</h1>
<div class="row">
<div class="col-sm-6">
<h4> <span class="special">Object Literal Syntax</span></h4>
<div class="code-block">
<code>const employee = { <br>
firstName : ‘Jane’, <br>
lastName : ‘Doe’<br>
};</code>
</div>
</div><!-- end of col-sm-6 -->
<div class="col-sm-6">
<h4> <span class="special">The "new" keyword with an Object constructor()</span></h4>
<div class="code-block">
<code>const employee = new Object();<br>
employee.firstName = 'Jane';<br>
employee.lastName = 'Doe'<br>
</code>
</div>
</div><!-- end of col-sm-6 -->
</div> <!-- end of row -->
<div class="row">
<div class="col-sm-6">
<h4><span class="special">The "new" keyword with an constructor function</span></h4>
<div class="code-block">
<code>
function employee(firstName, lastName){<br>
this.name = firstName;<br>
this.surname = lastName; <br>
}<br>
let employee1 = new employee ('Jane', 'Doe');<br>
</code>
</div>
</div> <!-- end of col-sm-6 -->
<div class="col-6">
<h4> <span class="special">Using the Object.create() method</span></h4>
<div class="code-block">
<code>
const employee = { <br>
position: "developer", <br>
hourly: 45, <br>
language: "JavaScript", <br>
snack: "",<br>
details(){<br>
console.log(this.position + " likes " + this.snack)<br>
}<br>
};<br>
const projectManager = Object.create(employee);<br>
projectManager.position = "Project manager";
projectManager.snack = "Chocolate";
projectManager.details();
</code>
</div>
</div> <!-- end of col-sm-6 -->
</div> <!-- end of row -->
</div><!-- end of container-fluid -->
<div class="container-fluid">
<h1>Using Objects</h1>
<div class="row">
<div class="col-sm-4">
<h4><span class="special">Accsessing an object's properties</span></h1>
<p>An object's properties can be accessed in 2 ways:</p>
<ol>
<li>Dot notation</li>
<li>Bracket notation
</ol>
<p>Dot notation</p>
<p class="code-block">objectName.propertyName</p>
<p>Bracket notation</p>
<p class="code-block"> objectName[propertyName]</p>
</div> <!-- end of col-sm-4 -->
<div class="col-sm-4">
<h4><span class="special">Deleting an object's properties</span></h4>
<li>Deleting an object's property with the "delete" keyword<li>
<p class="code-block">delete objectName.propertyName</p>
</div> <!-- end of col-sm-4 -->
<div class="col-sm-4">
<h4><span class="special">Modifying an object's property</span></h4>
<li>Dot notation
<p class="code-block"> objectName.newProperty = "Hello";
</li>
<li>Bracket notation
<p class="code-block"> objectName[newProperty] = "Hello";
</li>
<p>Note: You can also change an existing property's value with the above. Simply re-assign the new value</p>
</div> <!-- end of col-sm-4 -->
</div> <!-- end of row -->
</div> <!-- end of container -->
<div class="container-fluid">
<div class= "row">
<div class="col-sm-4">
<h4><span class="special">Looping through an object</span></h4>
<p>for-in loop</p>
<p>The for-in loop is used to iterate through an object's properties</p>
<div class="code-block">
<code>
for (let key in objectNAme) { <br>
console.log(objectName[key]);<br>
} <br>
</code>
</div>
</div> <!-- end of col-sm-4 -->
<div class="col-sm-8">
<h1> Try this!</h1>
<ol>
<li> Use object constructor function to code a function called "aboutMe"</li>
<li>This function will include the following properties:
<ul>
<li>name</li>
<li>lastName</li>
<li>likes</li>
<li>dislikes</li>
<li>method called "activities" which will alert your likes </li>
</ul>
</li>
<li>Declare a variable "person" and use the "new" keyword to create a new "aboutMe". With all the above properties</li>
</ol>
</div> <!-- end of col-sm-8 -->
</div> <!-- end of row -->
</div> <!-- end of container -->
@import url("https://fonts.googleapis.com/css?family=Inconsolata:400,700");
body{
font-family: Inconsolata, monospace;
border: 2px dashed black;
font-size: 1.2em;
}
.container-fluid{
margin-top: 15px;
}
h1{
color: #856584;
text-transform: uppercase;
text-align:center;
}
.special{
background-color:#a77aa3;
padding: 3px;
}
code{
color: black;
}
.code-block{
background-color: #F0F0F0
}
/*
#e7e7d1
#a399b2
#9083a9
#856584
#a77aa3
*/
const employee = {
position: "developer",
hourly: 45,
language: "JavaScript",
snack: "",
details(){
console.log(this.position + " loves " + this.snack)
}
};
const projectManager = Object.create(employee);
projectManager.position = "Project manager"; projectManager.snack = "Chocolate"; projectManager.details();
This Pen doesn't use any external JavaScript resources.