Javascript - How to create a library from a prototype

About

This page shows you how to create a library from a prototype

Example

  • The library
var Point = function(loc) {
   // The prototype creation is important to be recognized as an ''instanceof''
   var obj = Object.create(Point.prototype);
   obj.loc = loc;
   return obj;
}

// Prototype added after the var declaration otherwise it does not exist
Point.prototype.move = function(){
    this.loc++;
}
  • The main
var point1 = Point(1);
point1.move();
var point2 = Point(2);
point2.move();


console.log(Point.prototype.constructor);
console.log(point1.constructor);

console.log(point1 instanceof Point);





Discover More
Javascript - (Prototype Model|Prototypical object)

A prototype-based language has the notion of a prototypical object. Any object can: specify its own properties, either when you create it or at run time. be associated as the prototype for another...
Javascript - Library

library in Javascript. A library is a distribution format as a package that is created from a module bundle from a prototype The difference between a package and a library is that the modules are...



Share this page:
Follow us:
Task Runner