Javascript Talk at St. Louis OOSIG

Notes from my OOSIG talk on JavaScript, a classless (prototype-based) OO language

On October 16, 2003, I spoke on “Objects without Classes”, the non-class-based object model implemented by JavaScript and some other OO languages. The (brief) presentation is available here:

Talk in Powerpoint format

The plain text of the slides (which is, frankly, plenty good enough) follows:

Objects without Classes – JavaScript

An Alternative Object Model

Kyle Cordes

Oasis Digital Solutions Inc.

St. Louis OOSIG

Oct. 16, 2003

Yet Another “About Me” Slide

* Developer, Architect, Consultant, PM, etc.

* Learned OO in C++, early 1990s

* Many other OO languages since: Java, Delphi, C#, Python, Ruby,
JavaScript, etc.)

* Oasis Digital Solutions Inc.

¨ Outsourced development

¨ Services to help teams write better software

* kylecordes.com

Assumptions

* You write software

¨ Not necessarily a lot of software

* You use an OO language

¨ It’s hard to find one that isn’t nowadays

* (More technical than some OOSIG talks)

Agenda

* Objects and Classes

* Classless OO

* Very Brief Introduction to JavaScript / ECMAScript

* Examples

The Point

* Object Oriented does not necessarily mean Classes.

* Classless OO is a useful model, in surprisingly widespread
use, though not in mainstream enterprise development.

* Nonetheless, understanding classless OO can help you create
better designs in the language you use now.

Class-based OO

* C++, Java, C#, Smalltalk, Eiffel, Delphi, many more.

* Objects are divided into types, and don’t change type

* The properties of a set of objects of the same class
are specified in a class definition

* Classes are abstract thing, instances are specific

* Class relationship is static

* This is the only way it can be done, right?

Classless Objects

* Some OO langages don’t have classes

¨ Called Classless or Prototype-based

* Objects share behavior by sharing a common prototype object.

* All objects are instances

* “Class” relationship is dynamic

* Classless OO can be used to build class-based OO, it is more
inclusive

For Example…

* A few years ago, this would be of academic interest only

* But…

* JavaScript (ECMAScript) is in widespread use, and has classless
OO

¨ “Dice Index” of 964

¨ That’s more than C#, for example

Intro to Javascript

* Not much to do with Java

¨ Name came about from Netscape cashing in on initial Java excitement

* Standardized as ECMAScript

* Current spec version is 1.5.

Where is JavaScript Used?

* Most commonly, in client-side web scripting

* Web server implementations

* Very interesting Web Services toolkit from IBM

* In .NET, ships “in the box”

JavaScript Implementations

* Initial Netscape Implementation

* Rhino (written in Java)

¨ Easy to add script scripting to Java apps

* Mozilla

* IE (JScript)

* Opera etc.

* JScript.NET

JavaScript Features

* Dynamic typing, “duck typing”

* Type-safe (dynamic != unsafe)

* Single Inheritance

* Compiled to intermediate representation

¨ Can JIT to machine code, Jscript.NET

* No Classes, Prototype-based

* Functions are first-class objects

A Constructor is just a Function

* function Employee () {
this.name = “”;
this.dept = “general”;
}

* var x = new Employee; // instance

Constructor Parameters

* function Employee (name, dept) {
this.name = name;
this.dept = dept;
}

Methods

* function WFW() {
return this.wage * weeks;
}

* Employee.WageForWeeks = WFW;

Tighter Syntax for Methods

* function Employee () {
this.name = “”;
this.dept = “general”;

this.WageForWeeks =
function (weeks) {
return this.wage * weeks;
}
}

Subclasses

* function Manager () {
this.reports = [];
}
Manager.prototype = new Employee;

* function WorkerBee () {
this.projects = [];
}
WorkerBee.prototype = new Employee;

Subclasses (deeper)

* function SalesPerson () {
this.dept = “sales”;
this.quota = 100;
}
SalesPerson.prototype = new WorkerBee;

* It dynamic, we can change it later (example)

Add A Property

* Add to an instance:
var jim = new Employee;
jim.years_of_service = 12;

* Add to all instances:
Employee.prototype.phone = “”;

* (example)

AOP

* Aspect Oriented Programming

* With a prototype-based, dynamic language, we can add wrapper
/ “advice” etc. without needing new language features or compilers.

Singletons etc.

* There are many times in class based languages where you define
many of classes and create 0 or 1 instances of each, to hold some bit of code.

* With prototype-based OO, many of these situations get much
simpler.

OK, So It’s Different, So What?

* Classless teaches us to think about objects first, then classes
later

¨ Behavior, not just data

¨ Sequence Diagrams before Class Diagrams

* I have been building better designs sooner, since learning
JavaScript, even though I don’t use it much.

Back to The Point

* Object Oriented does not necessarily mean Classes.

* Classless OO is a useful model, in surprisingly widespread
use, though not in mainstream enterprise development.

* Nonetheless, understanding classless OO can help you create
better designs in the language you use now

Links

*
xhref=”http://devedge.netscape.com/central/javascript/” mce_href=”http://devedge.netscape.com/central/javascript/”>http://devedge.netscape.com/central/javascript/

Talk Offer

* I need practice giving talks!

* I’ve give this talk, or one like it

¨ In your organization

¨ For Developers, For Managers

¨ No Cost (in town), No Obligation

The End

Slides will be on my site:

* http://kylecordes.com