HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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();
Also see: Tab Triggers