Monday, December 24th, 2007
Organise Your Code With base2.Packages
<>p>Since Dean Edwards announced base2 beta he has been taking some time to describe it for us.His latest installment is on base2 packages:
A base2.Package provides a mechanism for bundling classes, constants and functions within a closure. You can define what symbols you want to export from the Package and you can define the symbols you want to import into the closure.
The package concept is used throughout base2 itself to manage components and namespaces.
The template for creating a package is:
-
-
new function(_) { // create the closure
-
// create the package object
-
var MyPackage = new base2.Package(this, {
-
name: "MyPackage",
-
version: "1.0",
-
imports: "SomeOtherPackage",
-
exports: "MY_CONSTANT,MyClass,myFunction"
-
});
-
-
// evaluate the imported namespace
-
eval(this.imports);
-
-
// define package contents
-
-
var MY_CONSTANT = 42;
-
-
var MyClass = SomeOtherClass.extend({
-
// class definition
-
});
-
-
function myFunction() {
-
return "Hello!";
-
};
-
-
// evaluate the exported namespace (this initialises the Package)
-
eval(this.exports);
-
};
-
and here is an example that uses one:
-
-
new function(_) { // create the closure
-
// create the package object
-
var graphics = new base2.Package(this, {
-
name: "graphics",
-
version: "1.0",
-
imports: "shapes",
-
exports: "Layout"
-
});
-
-
// evaluate the imported namespace
-
eval(this.imports);
-
-
// we can refer to the Rectangle class directly because we have imported the
-
// shapes Package.
-
var Layout = Rectangle.extend({
-
// I don't know anything about graphics
-
});
-
-
// evaluate the exported namespace (this initialises the Package)
-
eval(this.exports);
-
};
-








As for me – it’s uncomfortable thing…