Skip to content
This repository has been archived by the owner on Dec 28, 2017. It is now read-only.
Alex Shvets edited this page Apr 2, 2013 · 39 revisions

API documentation | GitHub | NPM package | Travis CI

This is the micro-framework that based on the ECMA Script 5 features like Object.create⠙ and property descriptors⠙.

It's trivial to create new "classes" - just clone the prototype and change a couple of properties and voila... new "class".

It's really class-free: do you know the difference between js constructor-functions and classes in other languages? $object.clone produces prototype objects, not function-constructors, unlike other class-producing tools (Backbone.Model.extend, Ext.define, dojo.declare).

In this framework you can easilly create and manipulate objects without constructors, instead of js way, where you should define a constructor for every object (that you want to use as prototype), even if you didn't need it. It's possible to build and maintain extremely large numbers of "classes" with comparatively little code.

See more advantages of prototype-based OOP⠙.

Installation

Node.js:

npm install clonejs

CDN⠙ for client-side (~3 KB gzipped):

<script src="http://quadroid.github.com/clonejs/cdn/clone.min.js"></script>

Usage

Node.js:

var ns = require('clonejs'),
    $object = ns.prototype;
    
// Forget about classes.    
// Instead of creating class (function), create prototype (object):
var $duck = $object.clone({
    quack: function(){}
});

// Forget about the `new` operator, use .create() method instead:
var duck = $duck.create();

// Forget about the `instanceof` operator, use JS native 
// .isPrototypeOf() method instead:
$duck.isPrototypeOf(duck);// - true
Quick example (cloning):
var $myProto = {a:1, b:2, c:3};
var    clone = $object.clone.apply($myProto);
     clone.a = 11; // $myProto.a still == 1
  $myProto.b = 22; // clone.b will be also changed to 22

See: clone, create, copy, deepCopy, deepClone.

Property modificators:
var $myType = $object.clone({
    '(final)          property1': "not configurable and not writable",
    '(writable final) property2': "not configurable only",
    '(hidden)         property3': "not enumerable",
    '(const)           constant': "not writable",
                      property4 : "simple property",
    '(get)       property3alias': 'property3',// automatically create getter
                          _item : "private property (not enumerable)",
                   '(get)  item': function() { return this._item },
                   '(set)  item': function(v){ this._item = v    },
                    constructor : 'MyType'
});
assert( $myType.property3alias === $myType.property3 );

See: describe.

Constructors & instantiation:
var $parent = $object.clone({
    constructor: function Parent(){
        console.log('$parent constructor arguments:', arguments);
    }
});
var $child = $parent.clone({
    constructor: function Child(arg){
        console.log('$child constructor arguments:', arguments);
        this.callSuper('constructor', arg);
    }
});
var $grandchild = $child.clone({
    constructor: function Grandchild(){
        console.log('$grandchild constructor arguments:', arguments);
        this.applySuper(arguments);
    }
});
var grandchild = $grandchild.create(1,2,3);
    // will output:
    // $grandchild constructor arguments: [1,2,3]
    // $child constructor arguments: [1,2,3]
    // $parent constructor arguments: [1]
    
assert( $child.isPrototypeOf(grandchild) );

See: constructor, create, applySuper, callSuper, createSuperSafeCallback.

Properties iteration:
var $obj = $object.clone({a: 11, b: 22, c: 33}),
     obj = $obj.create({d: 44});

var mappedObj = obj.map(function(value){return 100 + value}, obj, true);
    // mappedObj == {d: 144} // mapped only own property

var proto = {e: 55};
mappedObj = obj.map(function(value){return 100 + value}, obj, false, proto);
    // mappedObj == {a: 111, b: 122, c: 133, d: 144, e: 155}
proto.f = 66;
    // mappedObj == {a: 111, b: 122, c: 133, d: 144, e: 155, f: 66}

See: forEach, map, filter, every, some.

Namespaces:
ns.extend('collection', {
    $item:  {},
    _items: {}
});
ns.collection.extend('arrayCollection', {_items: []});

var $users = ns.collection.$arrayCollection.clone({
    $item: {name: '', isAdmin: false},
    constructor: function Users(items){
        items.forEach(function(item){
            var newItem = $object.create.call(this.$item, item);
            this._items.push(newItem);
        }, this);
    }
});
ns.put('models.users', $users);

var users = ns.models.$users.create([{name: 'User1'}]);

See: ns, ns.extend, ns.put.

yandex metrika githalytics.com alpha