Thursday, December 24th, 2009
Category: Closure, Google
Michael Bolin formerly of Google has created Closure Lite as a way to let developers get their feet wet in the Closure library without having to go into the compiler and the like (but they should do that eventually!).
Closure Lite itself consists of the following subset of APIs:
It is build via the closure compiler itself, building from a set of goog.require() calls :)
Friday, November 13th, 2009
Category: Closure, JavaScript
The library portion of Google's Closure not only gives us a new API to work with, but also adds another piece of mature code to the public domain, where it can be studied and scrutinized. Something we might normally have done with View Source if not for the compression techniques in effect.
A new article by Michael Bolin looks at inheritance patterns in Javascript, with a special focus on Closure. He does a thorough job of weighing up Closure's use of the pseudoclassical inheritance pattern against what Doug Crockford labels the functional pattern.
Pseudoclassical inheritance (Closure):
JAVASCRIPT:
-
-
/**
-
* @param {string} phoneNumber
-
* @param {string=} signature
-
* @constructor
-
* @extends {Phone}
-
*/
-
SmartPhone = function(phoneNumber, signature) {
-
Phone.call(this, phoneNumber);
-
-
/**
-
* @type {string}
-
* @private
-
*/
-
this.signature_ = signature || 'sent from ' + this.getPhoneNumber();
-
};
-
goog.inherits(SmartPhone, Phone);
-
-
/**
-
* @param {string} emailAddress
-
* @param {string} message
-
*/
-
SmartPhone.prototype.sendEmail = function(emailAddress, message) {
-
// Assume sendMessage() is globally available.
-
sendMessage(emailAddress, message + '\n' + this.signature_);
-
};
-
-
/** @override */
-
SmartPhone.prototype.getDescription() = function() {
-
return SmartPhone.superClass_.getDescription.call(this) +
-
' It can also send email messages.';
-
};
-
-
var smartPhone = new SmartPhone('5555555', 'Adios'};
-
Functional inheritance:
JAVASCRIPT:
-
-
var smartPhone = function(spec) {
-
var that = phone(spec);
-
spec.signature = spec.signature || "sent from " + that.getPhoneNumber();
-
-
that.sendEmail = function(emailAddress, message) {
-
// Assume sendMessage() is globally available.
-
sendMessage(emailAddress, message + "\n" + spec.signature);
-
};
-
-
var super_getDescription = that.superior("getDescription");
-
that.getDescription = function() {
-
return super_getDescription() + " It can also send email messages.";
-
};
-
-
return that;
-
};
-
-
var mySmartPhone = smartPhone({"phoneNumber": "5555555", "signature": "Adios"};
-
Tuesday, November 10th, 2009
Category: Closure
Stephen Thomas has been experimenting with Closure and the graphics libraries:
This does something similiar to what I do on Stark, my visual mud project [github page] that uses the Processing JS library and the HTML 5 <canvas> element, but instead of creating a draw function that is executed x amounts per seconds, you just call a draw function and assign it to a variable for future interactions. Here is the source code for this example (you can also just look at the current page's source):
JAVASCRIPT:
-
-
var graphics = goog.graphics.createGraphics(200, 150);
-
-
// define the colors for the squares and the dot
-
var square_fill = new goog.graphics.SolidFill('yellow');
-
var square_stroke = new goog.graphics.Stroke(2, 'green');
-
var dot_fill = new goog.graphics.SolidFill('blue');
-
var dot_stroke = new goog.graphics.Stroke(1, 'black');
-
-
// the dot's initial position
-
var dot = {x: 1, y: 1};
-
-
// properties
-
var size = 40;
-
var margin = 5;
-
var width = size - margin;
-
var num_rows = 3;
-
var num_cols = 4;
-
-
// draw the squares
-
for (var x = 0; x <num_cols; x++) {
-
for (var y = 0; y <num_rows; y++) {
-
graphics.drawRect(margin + x * size, margin + y * size, width, width, square_stroke, square_fill);
-
}
-
}
-
-
// draw the dot
-
dot['graphic'] = graphics.drawEllipse(margin + dot['x'] * size + width / 2, margin + dot['y'] * size + width / 2, width / 4, width / 4, dot_stroke, dot_fill);
-
-
// call if the dot's position changes
-
redraw_dot = function() {
-
dot['graphic'].setCenter(margin + dot['x'] * size + width / 2, margin + dot['y'] * size + width / 2);
-
}
-
-
// key event handler
-
var key_handler = new goog.events.KeyHandler(document);
-
var key_event = function (e) {
-
if (e.keyCode == goog.events.KeyCodes.UP && dot['y']> 0) {
-
dot['y'] -= 1;
-
} else if (e.keyCode == goog.events.KeyCodes.RIGHT && dot['x'] <= num_cols - 2) {
-
dot['x'] += 1;
-
} else if (e.keyCode == goog.events.KeyCodes.DOWN && dot['y'] <= num_rows - 2) {
-
dot['y'] += 1;
-
} else if (e.keyCode == goog.events.KeyCodes.LEFT && dot['x']> 0) {
-
dot['x'] -= 1;
-
}
-
redraw_dot();
-
}
-
-
// put everything together
-
goog.events.listen(key_handler, 'key', key_event);
-
graphics.render(document.getElementById('shapes'));
-