node package manager
It’s your turn. Help us improve JavaScript. Take the 2017 JavaScript Ecosystem Survey »



Writes objects without class, but in the prototype way.


npm install --save proto-scope

Here is a demo of using it:

proto = require 'proto-scope'
  • Inherent

Passing object to to create sub-class, passing object to to create instance:

human =
  init: -> @name = 'human race'
  give_name: (@name) ->
  introduce: -> console.log "this is #{@name}"
tom =
tom.introduce() # => "this is human race" 
man =
  speak: ->
    print 'speaks by'@name
dan =
dan.give_name 'Dan'
dan.introduce() # => "this is Dan" 
dan.speak() # => "speaks by Dan" 
  • Super
  init: ->
    console.log 'this is a'
  init: ->
    console.log 'this is b'
  init: ->
    console.log 'this is c'


the difference between new and as is: new contains @init?():

module.exports =
  as: (object) ->
    = object or {}
    o.__proto__ = @
  new: (object) ->
    = @as object

Also, we my need super, so dirty code comes:

module.exports =
  as: (object) ->
    = object or {}
    o.__called__ = undefined
    o.__proto__ = @
    for keyvalue of o
      = (key, value) ->
        if typeof value is 'function'
          if key in ['as''new''super']
          o[key= (args...) ->
            o.__called__ = key
            value.apply oargs
          o[key].toString = ->
      f keyvalue
  super: ->

Read this for more details: Also available for JavaScript if you like that kind of life style...


This can not be widely used, since __proto__ is not supported in some platforms.

Actually there are some problems implementing OOP with prototype, and more people choose using class, but prototype is a simple concept.

Learn from Lua

This is kind of syntax from Lua:

function Account:new (o)
    o = o or {}   -- create object if user does not provide one
    setmetatable(o, self)
    self.__index = self
    return o