Giter Site home page Giter Site logo

classy's Introduction

Build Status

Classy

'classy/object_extras'

Utility functions to have a better touch with objects:

var ObjectKit = require('classy/object_extras');
ObjectKit.extendGlobal();

extendGlobal

extend global variable Object

ObjectKit.extendGlobal();
// iterates object
Object.forEach(obj, function (key, value) {

});

// get values as array
var obj = {type: 'animal', wear: 'wool', lives: 9};
Object.values(obj) // => ['animal', 'wool', 9];

// more detail type of object
Object.realType(null) // => 'null'
Object.realType(new Date) // => 'date'
Object.realType([]) // => 'array'

// methods of object
Object.methods(obj);

// own properties
Object.properties(obj) // -> return own properties

// all properties of object
Object.allProperties(obj) // -> return all properties of object

// variables
Object.instance_variables(obj)

// array of instance variable names
Object.instance_variable_names(obj)

// same as ruby's ancestors
Object.ancestors(obj) // return array of inherited prototypes

// check if object is constructor
Object.isConstructor(obj);

Ruby like classes for Node.js

Api example

var Animal = {
  isAnimal: true,

};

var Cat = Classy.build('Cat', function (proto, mutator) {
  this.extend(Animal);

  proto.initialize = function() {
    this.birthtime = new Date;
  };

  mutator.getter(proto, 'age', function() {
    return new Date() - this.birthtime;
  });

  mutator.aliasProperty(proto, 'new_age', 'age');

  ['left', 'right'].forEach(function(side) {
    proto[side + 'Hand'] = side + " hand";
  });
});

Cat.classEval(function(proto) {
  proto.tailMode = "down";

  proto.tailUp = function () {
    this.tailMode = "up";
  };

  proto.tailDown = function () {
    this.tailMode = "down";
  };

  proto.moveTail = function() {
    this.tailMode = this.tailMode == "up" ? "down" : "up"
  }
});

var aCat = new Cat;

console.log('klassName', aCat.klassName);
console.log('instance_variables', aCat.instance_variables);
console.log('object_id', aCat.object_id);
console.log('object_id', (new Cat).object_id);
console.log('ancestors', Cat.ancestors);
console.log('parentKlass', Cat.parentKlass);
console.log('klass', aCat.klass);
console.log('Class.klass', Cat.klass);
console.log('is_a', aCat.is_a(aCat.klass));
console.log('tap', aCat.tap(function() { console.dir(this); }));
console.log('inspect', aCat.inspect());
console.log('#age', aCat.age);
console.log('#new_age', aCat.age);

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.