Giter VIP home page Giter VIP logo

namespace's Introduction

namespace

Build Status

Library provides an API for creating sets of namespaced properties for any given objects (frozen or not). This effectively may be used for creating fields that are not part of object's public API, which is useful for hiding internal details or for adding fields to an existing objects (Built-ins or not) without mutating them and there for any risks of naming conflicts.

Usage

It is recommended to use this library with enabled WeakMaps. On node that simply means running it with additional flag: node --harmony_weakmaps. If weak maps are not available library will fallback to using imperfect weak map shim.

var ns = require('namespace/core').ns
var internals = ns()

internals(publicAPI).secret = secret

Namespace may be used with multiple objects:

var observable = ns()

function Observable() {
  observable(this).observers = []
}
Observable.prototype.observe = function(observer) {
  observable(this).observers.push(observer)
}

Also, multiple namespaces can be used with a same object without any conflicts.

var pending = ns()

function Eventual() {
  Observable.call(this)
  pending(this).realized = false
}
Eventual.prototype = Object.create(Observable.prototype)
Eventual.prototype.realize = function realize(value) {
  if (!pending(this).realized) {
    obesrvable(this).observers.splice.forEach(function(observer) {
      observer(value)
    })
  }
}

Access to the namespaced properties can be shared with other code by simple handing a namespace function. Although doing this across modules is not recommended, for example instead of sharing pending namespace one could share following function instead:

exports.isRealized = function isRealized(value) {
  return pending(value).realized
}

Namespaced objects create parallel inheritance chain, or more simply:

var foo = ns()
var ancestor = {}

foo(ancestor) === foo(Object.create(ancestor)) // => true

Namespaces are simply a sugar on top of ES.next WeakMaps allowing you to associate sets of namespaced properties to an object via weak references.

Install

npm install namespace

namespace's People

Contributors

gozala avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

namespace's Issues

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.