Hi there!

I've found the way to make inheritance in javascript even better. It's 
based on __proto__ property,
so it would not work in old browsers. And I think it's mostly applicable in 
serverside scripts.

It works on a top of coffee-script's inheritance model, but can be easily 
applied in pure js.
While extending class CS defines prototypes chain and also copies class 
methods with

for key of parent child[key] = parent[key] if __hasProp_.call(parent, key)

In this way not enumerable properties aren't copied. Also if you change 
parent's method
it would not change in child.

It gets better when class methods are inherited via prototype chain. It can 
be done with
child.__proto__ = parent
In the case of CS we also need to drop all the copied properties.

This way you can implement some interesting features.
For example, class instance variable:

Object.defineProperty Parent, 'test', get: -> @_test if @hasOwnProperty 
'_test' set: (val) -> @_test = val Parent.test = 1 Parent.test # => 1Child.test 
# 
=> undefined Child.test = 2 Parent.test # => 1 Child.test # => 2

I've made a package <https://github.com/printercu/coffee_classkit> with 
this function and included method from ruby's module system:
include that uses append_features, extend using extend_objects
and hooks (inherited, included, extended).

It works on a top of basic CS syntax and doesn't break into global 
namespace.
For ease of use there is class including all the methods, so you can just 
inherit
from it and use all the methods in more familiar way.

classkit = require 'coffee_classkit' class Child extends 
Parentclasskit.extendsWithProto @ classkit.include @, Mixin
# or
class Example extends classkit.Module @extendsWithProto() @include Mixin

I've included ActiveSupport::Concern's analog:

class Mixin extends classkit.Module @extendsWithProto().concern() 
@includedBlock: -> # run in base class context @instanceVariable 'test' 
class @ClassMethods someClassMethod: -> someInstanceMethod: -> class Base 
extends classkit.Module @include Mixin @someClassMethod() 
(newBase).someInstanceMethod()

If you like you can find more examples in tests in repo, or take a look at
sketches of this project <https://github.com/printercu/costa> built with 
CoffeeClasskit.

What do you think about it?

Max

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to