Giter Site home page Giter Site logo

ctor's Introduction

ctor

A simple and familiar prototypal system.

var Ctor = require('ctor');

var Person = Ctor.extend({
  constructor: function Person(name, nickname) {
    this.name = name;
    this.nickname = nickname;
  }
  , sayHello: function() {
    return "Hey, I'm " + this.name + '. But you can call me ' + this.nickname; 
  }
});

var p = Person.create('Marco', 'polotek');

console.log(p.sayHello()); // Hey, I'm Marco. But you can call me polotek

var Employee = Person.extend({
  constructor: function Employee(name, nickname, job) {
    // The person constructor has already been called
    this.job = job;
  }
  , sayHello: function() {
    // No concept of "super". If you want a super call, be explicit
    return "Hello, my name is " + this.name + '. I work at ' + this.job;
  }
});

var e = Employee.create('Marco', 'polotek', 'Acme Novelties');

console.log(e.sayHello()); // 'Hello, my name is Marco. I work at Acme Novelties'

// (p instanceof Person) === true
// p.sayHello === Person.prototype.sayHello
// (e instanceof Employee) === true
// (e instanceof Person) === true
// (e.__proto__ instanceof Person) === true

ctor is a really simple prototypal object system that codefies a few simple constructs that I like.

  • Don't use new. Think of create as your object factory. This follows nicely from Object.create.
  • There is a constructor for initializing your objects. create calls this on every object.
  • Instead of "inheritance", there is simple extension. extend creates a new object factory with the following behavior
    • Proper prototype chain is set up.
    • The extended parent constructor will always be called implicitly. Initialization integrity is preserved.
    • No other fanciness. No "super".

ctor's People

Contributors

oscargodson avatar polotek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ctor's Issues

Only works with Node, not in the browser

I'll probably be able to send a PR, but filing a ticket for now. What I like about this is for one it's familiar (obviously), but it's super tiny. I was a little sad to see it requires Node, so it'd be cool to have this work on the browser and be able to simply plop it in and code.

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.