1.3.2-alpha • Published 11 years ago

clonejs v1.3.2-alpha

Weekly downloads
5
License
-
Repository
-
Last release
11 years ago

clone.js Build Status

[ 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⠙.

Try the true prototype-based OOP⠙

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" type="text/javascript"></script>

Usage

Node.js:

var ns = require('clonejs'),
    $object = ns.prototype;
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

1.3.2-alpha

11 years ago

1.3.1-alpha

11 years ago

1.3.0-alpha

11 years ago

1.1.0-alpha

11 years ago

1.0.1-alpha

11 years ago

1.0.0-alpha

11 years ago

0.7.4-beta

11 years ago

0.7.3-beta

11 years ago

0.7.2-05-beta

11 years ago

0.7.2-03-beta

11 years ago

0.7.2-02-beta

11 years ago

0.7.2-01

11 years ago

0.7.2-01-beta

11 years ago

0.7.2

11 years ago