Giter VIP home page Giter VIP logo

extendables's Introduction

extendables

Simple and elegant inheritance in JS.

Install

npm install extendables

Require

var Extendable = require('https!raw.github.com/Gozala/extendables/v0.2.0/extendables.js').Extendable

Usage

var Extendable = require('https!raw.github.com/Gozala/extendables/v0.2.0/extendables.js').Extendable
var Base = Extendable.extend({
  inherited: function inherited() {
    return "inherited property"
  },
  overridden: function overridden() {
    return "property to override"
  },
  // No supers by default, use prototype and be proud, but if you really want
  // super get one!
  _super: function _super() {
    return Object.getPrototypeOf(Object.getPrototypeOf(this))
  }
})
// Adding static method.
Base.implement = function implement(source) {
  // Going through each argument to copy properties from each source.
  Array.prototype.forEach.call(arguments, function(source) {
    // Going through each own property of the source to copy it.
    Object.getOwnPropertyNames(source).forEach(function(key) {
      // If property is already owned then skip it.
      if (Object.prototype.hasOwnProperty.call(this.prototype, key)) return null
      // Otherwise define property.
      Object.defineProperty(this.prototype, key,
                            Object.getOwnPropertyDescriptor(source, key))
    }, this)
  }, this)
}

var b1 = new Base
console.log(b1 instanceof Base)              // -> true
console.log(b1 instanceof Extendable)        // -> true
console.log(b1.inherited())                  // -> "inherited property"

var b2 = Base()                             // -> Works same as without `new`
console.log(b2 instanceof Base)             // -> true
console.log(b2 instanceof Extendable)       // -> true
console.log(b2.inherited())                 // -> "inherited property"


var Decedent = Base.extend({
  constructor: function Decedent(options) {
    this.name = options.name;
  },
  overridden: function override() {
    // I'd rather copied `overridden` with a diff name overriddenBase for
    // example or used `Base.prototype.overridden.call(this)`
    // But this works as well :)
    return "No longer " + this._super().overridden.call(this)
  },
  // overriddenBase: Base.prototype.overridden
})
Decedent.implement({
  bye: function bye() {
    return "Buy my dear " + this.name
  }
})

var d1 = new Decedent({ name: "friend" })
console.log(d1 instanceof Decedent)       // -> true
console.log(d1 instanceof Base)           // -> true
console.log(d1 instanceof Extendable)     // -> true
console.log(d1.inherited())               // -> "inherited property"
console.log(d1.overridden())              // -> No longer a property to override
console.log(d1.bye())                     // -> "Bye my dear friend"

extendables's People

Contributors

gozala avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.