Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @evturn/proto with all npm packages installed. Try it out:

var proto = require("@evturn/proto")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@evturn/proto v0.1.1

Prototypes as classes

Proto

Use prototypes as classes

  • Three built in properties - new, extend, mixin
  • No need for the new operator

$ npm install @evturn/proto

Initializing

Create an object with only built in properties

const Proto = require('@evturn/proto');

const newDude = Proto.extend({});
or

Create an object with additional properties

const Proto = require('@evturn/proto');

const newDude = Proto.extend({
  name: 'Br0metheus Dudequest',
  sup: function() {
    return `I am ${this.name}.`;
  }
});

Proto.extend()

Creating new objects

  • this is a prototype object used as a class
const newDude = Proto.extend({
  name: 'Br0metheus Dudequest',
  sup: function() {
    return `I am ${this.name}.`;
  }
});

const newerDude = newDude.extend({
  name: 'Quentin Vesclovious'
});

newerDude.sup();
// I am Quentin Vesclovious.

Proto.new()

Creating new instances

  • this is the prototype of the new instance
const newDude = Proto.extend({
  name: 'Br0metheus Dudequest',
  sup: function() {
    return `I am ${this.name}.`;
  }
});

const newerDude = newDude.new({
  name: 'Quentin Vesclovious'
});

newerDude.sup();
// I am Br0metheus Dudequest.

/**
 *  `newerDude` is an instance of `newDude`
 *
 *  `this` does not reference the instance (`newerDude`)
 *  `this` references the class it is an instance of (`newDude`)
 */

Proto.mixin(target, source)

Merge properties from a source object into a target object

  • returns the target object
  • will not redefine/overwrite shared properties
const undertaker = Proto.extend({
  tombstone: function() {
    return `Envision me pile driving you head first into the canvas`;
  }
});

const kane = Proto.extend({
  chokeslam: function() {
    return `Just gonna quickly throw you through the Spanish announcer's table`;
  }
});

kane.mixin(kane, undertaker);
kane.tombstone();

// Envision me pile driving you head first into the canvas

Thanks, br0.

Now go out there and be somebody.
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free