All Projects → insin → Concur

insin / Concur

Licence: mit
Sugar for infectious JavaScript inheritance, metaprogramming & mixins

Programming Languages

javascript
184084 projects - #8 most used programming language
metaprogramming
66 projects

Projects that are alternatives of or similar to Concur

Remeda
A utility library for JavaScript and TypeScript.
Stars: ✭ 774 (+5428.57%)
Mutual labels:  utility
Cue Maker
A simple and easy to use program that fetches original cue files for your roms.
Stars: ✭ 19 (+35.71%)
Mutual labels:  utility
Bonjourmadame
Say "Hello ma'am!"
Stars: ✭ 9 (-35.71%)
Mutual labels:  utility
Tip
Programmable tooltip that can be used with any Mac OS app
Stars: ✭ 798 (+5600%)
Mutual labels:  utility
Mini Observable
A mini implementation of TC39 observables, plus some utils!
Stars: ✭ 16 (+14.29%)
Mutual labels:  utility
Safeeyes
Protect your eyes from eye strain using this simple and beautiful, yet extensible break reminder
Stars: ✭ 919 (+6464.29%)
Mutual labels:  utility
Utils
A collection of useful PHP functions, mini classes and snippets that you need and can use every day.
Stars: ✭ 750 (+5257.14%)
Mutual labels:  utility
Executor
Watch for file changes and then execute command. Very nice for test driven development.
Stars: ✭ 14 (+0%)
Mutual labels:  utility
To Title Case
Convert a string to a title case.
Stars: ✭ 17 (+21.43%)
Mutual labels:  utility
Pothosblocks
A collection of core processing blocks
Stars: ✭ 7 (-50%)
Mutual labels:  utility
Git Repo
Git-Repo: CLI utility to manage git services from your workspace
Stars: ✭ 818 (+5742.86%)
Mutual labels:  utility
Vault2env
Small utility to transfer fields of a key in Vault into the environment
Stars: ✭ 6 (-57.14%)
Mutual labels:  utility
W10 Cleanser
Remove advertising, disable data collection, annoying notifications, default apps and more. Ideal for fresh installs of Windows 10 and those looking to purify their current installation.
Stars: ✭ 24 (+71.43%)
Mutual labels:  utility
Pie
🍕 Enjoy a slice! A utility library for dealing with slices and maps that focuses on type safety and performance.
Stars: ✭ 788 (+5528.57%)
Mutual labels:  utility
Object.reduce
Reduces an object to a value that is the accumulated result of running each property in the object through a callback. JavaScript/node.js utility.
Stars: ✭ 11 (-21.43%)
Mutual labels:  utility
Engauge Digitizer
Extracts data points from images of graphs
Stars: ✭ 754 (+5285.71%)
Mutual labels:  utility
Slowquitapps
Add a global delay to Command-Q to stop accidental app quits.
Stars: ✭ 916 (+6442.86%)
Mutual labels:  utility
Start Server And Test
Starts server, waits for URL, then runs test command; when the tests end, shuts down server
Stars: ✭ 879 (+6178.57%)
Mutual labels:  utility
Sketchdiff
💎 Generate visual differences between two Sketch files or a previous git commit
Stars: ✭ 14 (+0%)
Mutual labels:  utility
Cs.2click
🔊 A Better Audio Router for a Modular System.
Stars: ✭ 7 (-50%)
Mutual labels:  utility

Concur build status

Syntactic sugar for JavaScript inheritance, which takes two of the JavaScript Functions Of The Ages (extend() and inherits()), combines their power in a Backbone-style infectious inheritance function and allows for inheritance-time metaprogramming and mixins for those who need them.

Runs in browsers and Node.js.

Install

Browser bundles export a global Concur variable.

Node.js:

npm install Concur
var Concur = require('Concur')

Usage

Concur is sugar for JavaScript inheritance.

It deals with constructor functions, prototypes and prototype chains. It does not break instanceof and constructors created with Concur will not be instanceof Concur - it does not attempt to transplant paradigms from other languages into JavaScript, but it does try to provide a convenient means of manipulating prototypes while setting up inheritance.

Assuming the standard inherits(child, parent) function - which (*deep breath*) puts one constructor's prototype in another constructor's prototype chain, so objects created with the latter will have access to the former's prototype properties when property access walks the prototype chain - you might have an inheritance hierarchy set up something like this:

function inherits(child, parent) {
  function F() {}
  F.prototype = parent.prototype
  child.prototype = new F()
  child.prototype.constructor = child
}

function Widget(attrs) {
  this.attrs = attrs || {}
}

Widget.prototype.isHidden = false

function Input(attrs) {
  Widget.call(this, attrs)
}
inherits(Input, Widget)

Input.prototype.inputType = null

Input.prototype.render = function(name, value) {
  return React.createElement('input', {
    type: this.inputType
  , name: name
  , value: value
  })
}

function TextInput(attrs) {
  Input.call(this, attrs)
}
inherits(TextInput, Input)

TextInput.prototype.inputType = 'text'

You could start sugaring this by having Concur take care of setting up prototype inheritance and generating default constructors.

The Concur.extend() function returns a constructor function - either one you provide or a default which calls the "parent" constructor in the context of the object being created, passing along all given arguments - which you can continue to work with as above. The extend() function is attached to the resulting constructor before it is returned, so it can be further extended from using the same API.

Setting up a prototype by augmenting a constructor's prototype property-by-property is preferred by some as it doesn't introduce any extra levels of nesting, and gives each property plenty of room for scanning and documentation:

var Widget = Concur.extend({
  constructor: function(attrs) {
    this.attrs = attrs || {}
  }
})

Widget.prototype.isHidden = false

var Input = Widget.extend()

Input.prototype.inputType = null

Input.prototype.render = function(name, value) {
  return React.createElement('input', {
    type: this.inputType
  , name: name
  , value: value
  })
}

var TextInput = Input.extend()

TextInput.prototype.inputType = 'text'

To further sugar this, you could pass additional properties to the extend() function, which will augment the prototype for you.

You might prefer to do this only with data, rather than functions, or you might prefer the compactness of having the entire prototype definition as part of one statement (particularly for constructors with small prototypes) - for demonstration purposes, this example shows the latter:

var Widget = Concur.extend({
  isHidden: false,

  constructor: function(attrs) {
    this.attrs = attrs || {}
  }
})

var Input = Widget.extend({
  inputType: null,

  render: function(name, value) {
    return React.createElement('input', {
      type: this.inputType
    , name: name
    , value: value
    })
  }
})

var TextInput = Input.extend({
  inputType: 'text'
)}

Manipulating Prototypes

The following "special" properties, or "dunder-properties" owing to the double underscores, can be used to manipulate prototypes at inheritance time. The manipulations they enable are performed in the order they are listed below.

__meta__(prototypeProps, constructorProps)

If a constructor's prototype properties include a dunder-meta property, then when extend() is used on that constructor, dunder-meta will be called with all property-defining objects which were passed in.

This enables you to declare constructors which can modify the prototypes of constructors inheriting from them, at inheritance time.

Contrived example:

function NutAllergyProtectionMeta(prototypeProps) {
   var nutIndex = prototypeProps.ingredients.indexOf('nuts')
   if (nutIndex != -1) {
      prototypeProps.ingredients.splice(nutIndex, 1)
   }
}

var Bar = Concur.extend({
  __meta__: NutAllergyProtectionMeta,

  eat: function() {
    if (this.ingredients.indexOf('nuts') != -1) {
      console.log('You eat nuts. You die.')
    }
    else {
      console.log('You feel a bit dunder-meta.')
    }
  }
})

var NougatBar = Bar.extend({
   ingredients: ['sugar', 'egg whites', 'nuts']
})
>>> var snack = new NougatBar()
>>> snack.eat()
You feel a bit dunder-meta.

Actual examples:

__mixins__

If any properties object passed to extend() includes a dunder-mixins Array, each of its contents will be mixed into that properties object in the given order.

API

Concur.extend([prototypeProps[, constructorProps]])

Creates a child constructor which inherits from the call context object (this), adding the given prototype and constructor properties and adding extend() as a property of the new constructor for further extension:

  • Calling Concur.extend() creates a "base" constructor, which inherits from Object just like any other Function.

  • Calling extend() in the context of any other constructor creates a new constructor which inherits from it.

When required, constructor logic should be provided as a function - prototypeProps.constructor() - otherwise, a default constructor which calls the parent constructor with all given arguments will be created for you.

Child constructors will have a __super__ property added to them referencing the prototype they extend, as a convenience for accessing it when required.

Child constructors will also have an __mro__ property added to them, which is a list of the constructors in their inheritance chain, with the new child constructor itself at the head of the list.

Special arguments

prototypeProps.constructor([...])

If provided, this function will be used as the child constructor, otherwise a new child constructor function will be created for you.

prototypeProps.__meta__(prototypeProps, constructorProps)

If provided, this function will not be used immediately, but will be called when further extension is done based on the constructor returned by this call to extend().

At that point, __meta__ will be called with the property arguments passed to extend() so it can customise them before they're used to set up the inheriting constructor's prototype.

prototypeProps.__mixins__ and constructorProps.__mixins__

If provided, the contents of this Array will be mixed in to the properties object it's set on, in the given order.

MIT Licensed

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].