Friday, May 3, 2013

Java script - Prototype and new


Basically I was looking for how 'new' and 'prototype' things works out in Java script, and I could find very good site, which has lot of stuff on these areas.

http://ejohn.org/apps/learn/#65

Example #1:


RecordManager= function(){};

RecordManager.prototype.getRecords = function(){
  return true;
};

var manager = new RecordManager();

assert(manager.getRecords(), "status");

OutputPASS status


Example #2:


function RecordManager(){}

RecordManager.prototype.addRecords= function(){
  return true;
};

var manager1 = RecordManager();
assert( manager1 , "Is undefined, not an instance of RecordManager." );

var manager2= new RecordManager();
assert( manager2.addRecords(), "Method exists and is callable." );

Output:

  1. FAIL Is undefined, not an instance of RecordManager.
  2. PASS Method exists and is callable.





No comments:

Post a Comment