Giter VIP home page Giter VIP logo

tangramjs's Introduction

Tangram.js

A library for class-free object-oriented programming in JavaScript.

I used to think that the important innovation of JavaScript was prototypal inheritance. I now think it is class-free object-oriented programming. I think that is JavaScript’s gift to humanity. That is the thing that makes it really interesting, special, and an important language. -- Douglas Crockford

license npm downloads

build code climate coverage code climate dependencies devDependencies

Sauce Test Status

Contents

Getting Started

Installation

npm install tangramjs --save

Integration

var createObject = require('tangramjs').createObject;

API

createObject

This is the immutable base factory of Tangram.js. It has no property descriptors, and thus, it can only create new immutable empty objects.

factory([spec])

Creates a new object which has an immutable set of own properties and no prototype.

With the optional argument spec the default values of specific properties, writable or not, can be overwritten at construction time.

factory.val(name[, defaultValue])

Creates a new immutable factory, copying all existing property descriptors and the new one. The new property descriptor takes precedence over the existing ones and describes an enumerable and non-writable property.

factory.var(name[, defaultValue])

Creates a new immutable factory, copying all existing property descriptors and the new one. The new property descriptor takes precedence over the existing ones and describes an enumerable and writable property.

factory._val(name[, defaultValue])

Creates a new immutable factory, copying all existing property descriptors and the new one. The new property descriptor takes precedence over the existing ones and describes an non-enumerable and non-writable property.

factory._var(name[, defaultValue])

Creates a new immutable factory, copying all existing property descriptors and the new one. The new property descriptor takes precedence over the existing ones and describes an non-enumerable and writable property.

factory.ext(otherFactory)

Creates a new immutable factory, copying all property descriptors of both factories. The property descriptors of the left-hand side factory are taking precedence over the other ones.

References

Examples

An Alligator, a Duck, and a Goat

Creating a new factory called createAnimal:

var createAnimal = createObject
    .val('name', 'animal');

Creating a new factory called createFlyingAnimal:

var createFlyingAnimal = createAnimal
    ._val('fly', function () {
        console.log(this.name + ' makes flap flap');
    });

Creating a new factory called createSwimmingAnimal:

var createSwimmingAnimal = createAnimal
    ._val('swim', function () {
        console.log(this.name + ' makes splish splash');
    });

Creating a new factory called createTalkingAnimal:

var createTalkingAnimal = createAnimal
    .val('word', '...')

    ._val('talk', function () {
        console.log(this.name + ' says ' + this.word);
    });

Creating a new factory called createWalkingAnimal:

var createWalkingAnimal = createAnimal
    ._val('walk', function () {
        console.log(this.name + ' makes stomp stomp');
    });

Creating a new factory called createAlligator:

var createAlligator = createSwimmingAnimal
    .ext(createTalkingAnimal)
    .ext(createWalkingAnimal)

    .val('name', 'alligator')
    .val('word', 'grrr');

Creating a new factory called createDuck:

var createDuck = createFlyingAnimal
    .ext(createSwimmingAnimal)
    .ext(createTalkingAnimal)
    .ext(createWalkingAnimal)

    .val('name', 'duck')
    .val('word', 'quack');

Creating a new factory called createGoat:

var createGoat = createTalkingAnimal
    .ext(createWalkingAnimal)

    .val('name', 'goat')
    .val('word', 'baa');

Creating and using a new immutable object called alligator:

var alligator = createAlligator();

alligator.swim(); // alligator makes splish splash
alligator.talk(); // alligator says grrr
alligator.walk(); // alligator makes stomp stomp

Creating and using a new immutable object called duck:

var duck = createDuck();

duck.fly(); // duck makes flap flap
duck.swim(); // duck makes splish splash
duck.talk(); // duck says quack
duck.walk(); // duck makes stomp stomp

Creating and using a new immutable object called goat:

var goat = createGoat();

goat.talk(); // goat says baa
goat.walk(); // goat makes stomp stomp

Creating an object that behaves like it inherits from Object.prototype

Creating a new factory called createStandardObject:

var createStandardObject = createObject
    ._val('hasOwnProperty', Object.prototype.hasOwnProperty)
    ._val('propertyIsEnumerable', Object.prototype.propertyIsEnumerable)
    ._val('toLocaleString', Object.prototype.toLocaleString)
    ._val('toString', Object.prototype.toString)
    ._val('valueOf', Object.prototype.valueOf);

Creating a new factory called createPerson:

var createPerson = createStandardObject
    .val('name', 'John Doe')
    .val('age', 0);

Creating and using a new immutable object called person:

var person = createPerson({
    name: 'Jane Doe',
    age: 99
});

console.log(person.hasOwnProperty('name')); // true
console.log(person.propertyIsEnumerable('name')); // true
console.log(person.toLocaleString()); // [object Object]
console.log(person.toString()); // [object Object]
console.log(person.valueOf()); // { name: 'Jane Doe', age: 99 }

tangramjs's People

Contributors

clebert avatar

Watchers

 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.