pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/Hyuk/javascript/blob/master/javascript-basics/javascript-basics/constructors.md

ce6c8e711.css" /> javascript/javascript-basics/javascript-basics/constructors.md at master · Hyuk/javascript · GitHub
Skip to content

Latest commit

 

History

History
192 lines (154 loc) · 3.23 KB

File metadata and controls

192 lines (154 loc) · 3.23 KB

Constructors

Constructors are functions that create new objects.

Example of Constructor

function Bird() {
    this.name = "Albert";
    this.color = "blue";
    this.numLegs = 2;
}

Example of Constructor

function Dog(name, color) {
    this.name = name;
    this.color = color;
    this.numLegs = 4;
}

var myDog = new Dog("Sarang", "Brown");

instanceof

Verify if the variable is created based on constructor

let Bird = function(name, color) {
    this.name = name;
    this.color = color;
    this.numLegs = 2;
}
let crow = new Bird("Alexis", "Black");

crow instanceof Bird;
// true

let canary = {
    name: "Mildred",
    color: "Yellow",
    numLegs: 2
};

canary instanceof Bird;
// false

hasOwnProperty

Check the new object's property and add new property.

function Bird(name) {
  this.name = name;
  this.numLegs = 2;
}

let canary = new Bird("Tweety");
let ownProps = [];
// Add your code below this line
for (let property in canary) {
  if(canary.hasOwnProperty(property)) {
    ownProps.push(property);
  }
}
ownProps.push("own");

Prototype

add universal properties on object created based on constructor

function Dog(name) {
  this.name = name;
}
let beagle = new Dog("Snoopy");

Dog.prototype.numLegs = 4;

Prototype properties

get properties and prototype properties from constructors

function Dog(name) {
  this.name = name;
}

Dog.prototype.numLegs = 4;

let beagle = new Dog("Snoopy");

let ownProps = [];
let prototypeProps = [];

// Add your code below this line 


for(let property in beagle) {
  if (beagle.hasOwnProperty(property)){
    ownProps.push(property);
  } else {
    prototypeProps.push(property);
  }
}

constructor property in Constructors

there are consturctor property in Consturctor which is equal to the Constructor

function Dog(name) {
  this.name = name;
}

// Add your code below this line
function joinDogFraternity(candidate) {
  if(candidate.constructor === Dog) {
    return true;
  } else {
    return false;
  }
}

add mutiple prototype properties at once - using object

function Dog(name) {
  this.name = name; 
}

Dog.prototype = {
  // Add your code below this line
  constructor: Dog, // Mandatory
  numLegs: 4,
  eat() {
    console.log("yam yam yam");
  },
  describe() {
    console.log("My name is " + this.name);
  }
};

isPrototypeOf

function Dog(name) {
  this.name = name;
}

let beagle = new Dog("Snoopy");

// Add your code below this line

Dog.prototype.isPrototypeOf(beagle);

Inheritance

function Animal() { }

Animal.prototype = {
  constructor: Animal,
  eat: function() {
    console.log("nom nom nom");
  }
};

function Dog() { }

// Add your code below this line
Dog.prototype = Object.create(Animal.prototype); // set dog's prototype using animal's prototype

let beagle = new Dog();
beagle.eat();  // Should print "nom nom nom"

Private Property

function Bird() {
  let weight = 15; // private property
  this.getWeight = function() {
    return weight;
  }
  
}

IIFE (Immediately Invoked Function Expression)

(function () {
  console.log("A cozy nest is ready");
})();
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy