Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h2>JS Basics: Object Oriented Design</h2>
<i>Basic Object Oriented Design examples in JS</i>
</br>
</br>
<div id="here"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                window.onload = () => {
  
  const print = (msg) => {
    const br = document.createElement("br")
    const el = document.getElementById("here")
    const tn = document.createTextNode(msg)
    el.appendChild(tn)
    el.appendChild(br)
    console.log(msg)
  } 
  
  //Prototype (no class keyword syntactic sugar)
  const Person = function(name, age) {
    this.name = name
    this.age = age
  }
  
  Person.prototype.gender = "MALE"
  Person.prototype.title = "Mr."
  
  let me = new Person("Adam", 34)
  me.gender = "MALE"
  me.title = "Mr. Man Guy"
  
  print(me.title)
  print(me.gender)
  print(me.name)
  print(me.age)
  
  /**
   * Prototype hierarchy
   */
  const Dog = function(bark, name) {
    this.name = name
  	this.bark = bark
  }
  const fido = new Dog("woof", "fido")
  Dog.prototype.tail = function(){
  	print(`${this.name} wags and says ${this.bark}`)
  }
  fido.tail()
  
  //Now extend the Dog prototype with the Poodle prototype
  //Do so by setting the prototype attribute
  const Poodle = function(bark, name, haircut) {
  	this.name = name
  	this.bark = bark
    this.haircut = haircut
  }
  Poodle.prototype = Object.create(Dog.prototype)
  
  const doras = new Poodle("bark bark", "doras", "puffy")
  doras.tail()
  print(doras.haircut)
  print(doras instanceof Dog)
  print(doras instanceof Poodle)
  
  /////////////////////////////////////////////////////////////////////////////////////
  
  // Working with classes - these are prototypes under the hood
  class Cat {
	  constructor(meow) {
		  this.meow = meow
	  }
  }
  const kitten = new Cat("meow")
  print(kitten.meow)
  
  /**
   * Extend the class with Sphinx
   */
  
  class Sphinx extends Cat {
    constructor(meow, tomb) {
      super(meow)
		  this.guardianOf = tomb
	  }
  }
  
  const guardian = new Sphinx("rawr", "pyramids")
  print(guardian.meow)
  print(guardian.guardianOf)
  print(guardian instanceof Cat)
  print(guardian instanceof Sphinx)
  
  /////////////////////////////////////////////////////////////////////////////////////
  
  //Quick way - not recommended for backend or data requiring heavy-validation 
  //Note the lack of extends, new, or class keyword
  //Instead we define the type as a field
  const localeObjectFactory = (city, state) => ({
    "city": city,
    "state": state,
    "type": "LOCALE"
  })
  
  const seattle = localeObjectFactory("Seattle", "WA")
  print(seattle.city)
  print(seattle.state)
  print(seattle.type === "LOCALE")
  
  const subType = (city, state, country) => {
    let x = localeObjectFactory(city,state)
    //can make this an array if both types are required to be present
    x.type = "WORLDWIDE"
    x.country = country
    return x
  }
  
  const seattleCountry = subType("Seattle", "WA", "USA")
  print(seattleCountry.city)
  print(seattleCountry.state)
  print(seattleCountry.country)
  print(seattleCountry.type === "LOCALE")
  print(seattleCountry.type === "WORLDWIDE")
  
}
              
            
!
999px

Console