Hi, Jeremy

Neko is prototype-based language. This means that objects are inherited from
other objects i.e. "prototypes" :-)
My way to implement classical OOP inheritance in Neko is following:

[code]
// class helper function
class = function (parent) {
   var child = $new(null);
   $objsetproto(child, parent);
   return child;
}

// let's declare classes

// Document -- base class, has no parent class
Document = $new(null);

// Document's constructor
Document.init = function (title) {
   this.title = title;
   return this;
}

// let's create new class from Document
Article = class(Document);

// Article constructor
Article.init = function (title, author) {
   this.author = author;
   $call($objgetproto(this).init, this, $array(title));
   return this;
}

Book = class(Article);

Book.init = function (title, author, pagesize) {
   this.pagesize = pagesize;
   $call($objgetproto(this).init, this, $array(title, author));
}

// $new() wrappers for better code readability
@Document = function (title) {
   return $new(Document).init(title);
}

@Article = function (title, author) {
   return $new(Article).init(title, author);
}

@Book = function (title, author, pagesize) {
   return $new(Book).init(title, author, pagesize);
}

// getting real objects :-)
var a = @Article("Bizz @ speed of tought", "Bill Gates");
var a2 = @Book("My Book", "Me", "A4");

$print(a, "\n");
$print(a.title, "\n");
$print(a2, "\n");
$print(a2.title, "\n");

$print("a.pagesize = ", a.pagesize, "\n");
$print("a2.pagesize = ", a2.pagesize, "\n");

[end of code]

--

BR,
Vitali Falileev
http://blog.insideable.com
ICQ: 75008864

2007/6/14, Jeremy Smith <[EMAIL PROTECTED]>:

What is the proper way of creating ane inheritance structure in neko?  Do
you clone the prototype of a type, add to it, and assign it to the
inherited type?  Or is there some other language construct for achieving
this?

Neko looks awesome, easy to extend, and insanely fast.  What a great
idea!  Kudos to the developers.

Thanks,
Jeremy

--
Neko : One VM to run them all
(http://nekovm.org)

-- 
Neko : One VM to run them all
(http://nekovm.org)

Reply via email to