Skip to content

Instantly share code, notes, and snippets.

View awilson28's full-sized avatar

Ayana Wilson awilson28

  • United States
View GitHub Profile
function Vehicle (type){
this.type = type || 'car';
}
console.log(Vehicle.prototype) // {} a.k.a empty object
Vehicle.prototype.getType = function(){
return this.type;
}
console.log(Vehicle.prototype) // {getType: [Function]} a.k.a an object with one method defined on its prototype object
function Car () {}
console.log(Car.prototype) // {}
function Vehicle (type){
  this.type = type || 'car'; 
}
console.log(Vehicle.prototype)  // {} a.k.a empty object
Vehicle.prototype.getType = function(){
  return this.type; 
}
console.log(Vehicle.prototype)  // {getType: [Function]} a.k.a an object with one method defined on its prototype object
function Car () {}
//functions can refer to variables defined anywhere in that function's scope chain
function makeSandwich(){
var magicIngredient = 'peanut butter';
//the inner make function refers to magicIngredient, a variable defined in the outer makeSandwich function
function make(filling){
return magicIngredient + ' and ' + filling;
}
return make('jelly');
}
@awilson28
awilson28 / Closure.js
Last active June 11, 2017 22:48
Examples of closure courtesy of Effective JavaScript by David Herman
//examples courtesy of Effective JavaScript by David Herman
//functions can refer to variables defined anywhere in that function's scope chain
function makeSandwich(){
var magicIngredient = 'peanut butter';
function make(filling){
return magicIngredient + ' and ' + filling;
}
var arraylike = document.getElementsByTagName('p')
//we cannot do ‘typeof arraylike’ because this returns ‘object’ not ‘array’
Array.isArray(arraylike)   // false 
var realArr = Array.prototype.slice.call(arraylike)
Array.isArray(realArr)   //true
var parent = {
  name: 'Dad'
}; 

/* With ECMAScript 5, the prototypal inheritance pattern 
formally entered the language via Object.create() */

var child = Object.create(parent)
@awilson28
awilson28 / __proto__.md
Last active November 5, 2016 03:35
__proto__ (exercise to be completed in your browser's console)
//DO THIS IN YOUR BROWSER'S CONSOLE

function Parent () {}

/* all functions be default get a public, nonenumerable property on them called PROTOTYPE, 
which is an object that possess a constructor key/property that is a reference to the 
function itself, Parent in this case. This object is called the Parent's prototype. Recall 
that nothing distinguishes a function that is used as a constructor from a function 
function Shelter () {}
var cabin = new Shelter()
cabin.__proto__              // Shelter {}
function Residence () {}
cabin.__proto__ = Residence.prototype
cabin.__proto__             // Residence {}
1. To your first question regarding the difference between JavaScript and class-based languages like Java:
Constructor functions in JS do in fact provide the same functionality that classes provide to Java. The difference lies in the extent to which Java fundamentally depends on classes. For instance, in Java, every object is an instance of a specific class. An object literally cannot be created if the class for it doesn’t exist. In JavaScript, unlike in Java, we can create an object super easily:
var newObject = {};
newObject.__proto__ // Object {}
Try this out in your console. This simple object declaration prompts the JavaScript engine to set newObject’s __proto__ property to Object. This happens behind the scenes. This __proto__ property enables newObject to enjoy the prototype methods defined for all Objects. You can check out these built in methods here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
//run this in your console
for (var i = 0; i <= 3; i++) {
  setTimeout(function(){
    console.log(i); 
  }, 0); 
}