Prototypes in Javascript

First class functions?

  • Create new functions from preexisting functions at run-time
  • Store functions in collections
  • Use functions as arguments to other functions
  • Use functions as return values of other functions

A language supports first class functions if:

What is an Object

//this is an object
var myObject = {};

//you can add properties(values) and methods(functions) to an object

myObject.name = 'Sam Object';

myObject.whatIsMyName = function() {
    return this.name;
}

//That's it.

What's wrong with this Object?

I can't reuse it or make multiple copies of it.

KWOW THESE WORDS

  • CLASS/FUNCTION
  • OBJECT
  • INSTANCE

...AND WHY THEY ARE DIFFERENT

ALSO IMPORTANT

  • INHERITANCE
  • CONSTRUCTOR
  • CONTEXT

http://stackoverflow.com/questions/17525450/object-vs-class-vs-function

What Are Prototypes

  • Prototypes are JUST Functions
    • new keyword is what depicts the difference between JUST a function and a constructor
  • Prototypes are SINGULAR
    • Prototype functions can have different CONTEXT but are same instance of function. This has a small memory footprint.
  • EVERY object has a prototype
  • Every object inherits from Object..and it's prototype
    • This is called prototypical inheritance​

Things to Know about Prototypes

  • Adding a new prototype to a class will affect ALL instances of the class even if they are already created.
  • Adding a new function to a class will affect ONLY that SINGLE instance of a class.
  • Instance members of the same name will OVERRIDE the prototype member of the same name. This happens because of order. Prototype happens first. Constructor second.
  • Prototype is a property of the constructor

The prototype Inheritance Chain

  • All objects that inherit from another object receive all prototype properties of all objects up the prototype chain
  • Adding to a prototype anywhere in the prototype chain will add property all the way up the chain.
  • hasOwnProperty() will tell you if the method is defined on the current object or inherited.
  • Inheritance ONLY inherits prototypes not properties.
  • Example: forEach example to support legacy browsers
if (!Array.prototype.forEach) {
 Array.prototype.forEach = function(callback, context) {
     for (var i = 0; i < this.length; i++) {
         callback.call(context || null, this[i], i, this);
     }
 };
}

Things to know

  • NEVER extend the Object prototype.
  • Prototype definitions are not hoisted because of assignment
  • __proto__ : now forget about it.
  • Since a constructor is just a function you can call it like a method without new.
    • Name your classes upper case as a visual signal.
    • This will make the context of the object the global scope.
    • Example of human proofing:
function User(first, last) {
 if (!(this instanceof arguments.callee)) {
     return new User(first,last);
 }
 this.name = first + " " + last;
}

Prototypes in Javascript

By Json Sewell

Prototypes in Javascript

  • 1,884