P
Peter Michaux
Douglas Crockford doesn't seem to like JavaScript's built-in syntax
for building new objects based on a prototype object. The constructor
function, its prototype property and the "new" keyword all seem very
offensive to him. For over a year, Crockford has proposed an alternate
way of using prototypes like this
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
var newObject = object(oldObject);
I can see some appeal in Crockford's technique above. I do see some
inefficiency in the code above as the little dance with F() and its
prototype needs to occur for every object created.
What concerns me more is his suggested use in the above link of
object() function in combination with what he calls "maker functions"
and "parasitic inheritance".
-------
When I first watched Crockford speak about parasitic inheritance it
took something similar to the following form where person() and
employee() are maker functions.
function person(first) {
return {
first: first,
getName: function() {
return this.first;
}
};
}
function employee(first, position) {
var that = person(first);
that.position = position;
that.getPosition = function () {
return this.position;
};
return that;
}
var giselle = employee("Giselle", "hot model");
giselle.getName();
The above code is inefficient as the functions objects referenced by
the getName and getPosition are not shared by all employee objects.
This leads to triple memory use compared with a standard JavaScript
formulation using a function as a constructor with the getName and
getPosition functions as properties of the constructor's prototype.
-------
Now Crockford is recommending his prototypal system with the object()
function in combination with parasitic inheritance. I haven't seen
clear examples where he is augmenting functions in the "prototype
object chain". It seems he has used parasitic inheritance more for
augmenting non-function valued properties: building up a hash. I am
not clear how Crockford would implement his recommendation with
multiple levels of makers where functions are augmented but he does
mention "secret" variables and privileged function properties. This
seems to lead in the direction of inefficiencies of the parasitic
example above where the privilaged function objects cannot be shared.
------
A while ago I played with using prototypes a different way than I
normally do to really emphasis the building up of complete prototype
objects. Below is my thinking translated to use Crockford's object()
function. In this way the getName and getPosition functions are shared
in memory.
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
// build up a prototypical person
var adam = {
first: 'Adam',
getName: function() {
return this.first;
}
}
// a person maker function
function person(first) {
var p = object(adam);
p.first = first;
return p;
}
// build up a prototypical employee
var homer = person('homer');
homer.position = 'safety';
homer.getPosition = function() {
return this.position;
}
// an employee maker function
function employee(first, position) {
var e = object(homer);
e.first = first;
e.position = position;
return e;
}
var giselle = employee("Giselle", "hot model");
giselle.getName();
---------
The above example could easily be written in the normal JavaScript way
without the need for the object() function.
var adam = {
first: 'Adam',
getName: function() {
return this.first;
}
}
function Person(first) {
this.first = first;
}
Person.prototype = adam;
var homer = new Person('homer');
homer.position = 'safety';
homer.getPosition = function() {
return this.position;
}
function Employee(first, position) {
this.first = first;
this.position = position;
}
Employee.prototype = homer;
var giselle = new Employee("Giselle", "hot model");
giselle.getName();
It seems to me that this last example is quite similar to the previous
example. This example is, in fact, more declarative than the previous
example because in this last example there are lines like
"Person.prototype = adam;" which says what it means quite clearly.
This example is also more efficient (although perhaps just slightly)
without the need for the "F() dance" in object(). This example uses
language-level constructs which which JavaScript programmers are
familiar. Even disregarding the object() function's length, this
example is shorter.
-------
Do you see advantages to using Crockford's object() function?
Do you see the apparent ugliness that Crockford seems to see in the
JavaScript built-in system of constructor functions, their prototype
objects and the "new" keyword?
Is there precedence for Crockford's object() function from another
language?
Should the JavaScript language have an facility like Crockford's?
object() function?
How would you code this Person-Employee example?
If you happen to be Douglas Crockford, how badly have I misinterpreted
your writing and speaking?
======================================================
REFERENCES
Crockford's page containing the object() function
<URL: http://javascript.crockford.com/prototypal.html>
Crockford's page mentioning parasitic inheritance
<URL: http://javascript.crockford.com/inheritance.html>
Crockford videos mentioning parasitic inheritance
See the "Advanced JavaScript" series on YUI blog
<URL: http://developer.yahoo.com/yui/theater/>
JSLint
<URL: http://jslint.com>
Crockford's Top Down Operator Precedence article
Precursor to his chapter in "Beautiful Code"?
<URL: http://javascript.crockford.com/tdop/tdop.html>
for building new objects based on a prototype object. The constructor
function, its prototype property and the "new" keyword all seem very
offensive to him. For over a year, Crockford has proposed an alternate
way of using prototypes like this
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
var newObject = object(oldObject);
I can see some appeal in Crockford's technique above. I do see some
inefficiency in the code above as the little dance with F() and its
prototype needs to occur for every object created.
What concerns me more is his suggested use in the above link of
object() function in combination with what he calls "maker functions"
and "parasitic inheritance".
-------
When I first watched Crockford speak about parasitic inheritance it
took something similar to the following form where person() and
employee() are maker functions.
function person(first) {
return {
first: first,
getName: function() {
return this.first;
}
};
}
function employee(first, position) {
var that = person(first);
that.position = position;
that.getPosition = function () {
return this.position;
};
return that;
}
var giselle = employee("Giselle", "hot model");
giselle.getName();
The above code is inefficient as the functions objects referenced by
the getName and getPosition are not shared by all employee objects.
This leads to triple memory use compared with a standard JavaScript
formulation using a function as a constructor with the getName and
getPosition functions as properties of the constructor's prototype.
-------
Now Crockford is recommending his prototypal system with the object()
function in combination with parasitic inheritance. I haven't seen
clear examples where he is augmenting functions in the "prototype
object chain". It seems he has used parasitic inheritance more for
augmenting non-function valued properties: building up a hash. I am
not clear how Crockford would implement his recommendation with
multiple levels of makers where functions are augmented but he does
mention "secret" variables and privileged function properties. This
seems to lead in the direction of inefficiencies of the parasitic
example above where the privilaged function objects cannot be shared.
------
A while ago I played with using prototypes a different way than I
normally do to really emphasis the building up of complete prototype
objects. Below is my thinking translated to use Crockford's object()
function. In this way the getName and getPosition functions are shared
in memory.
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
// build up a prototypical person
var adam = {
first: 'Adam',
getName: function() {
return this.first;
}
}
// a person maker function
function person(first) {
var p = object(adam);
p.first = first;
return p;
}
// build up a prototypical employee
var homer = person('homer');
homer.position = 'safety';
homer.getPosition = function() {
return this.position;
}
// an employee maker function
function employee(first, position) {
var e = object(homer);
e.first = first;
e.position = position;
return e;
}
var giselle = employee("Giselle", "hot model");
giselle.getName();
---------
The above example could easily be written in the normal JavaScript way
without the need for the object() function.
var adam = {
first: 'Adam',
getName: function() {
return this.first;
}
}
function Person(first) {
this.first = first;
}
Person.prototype = adam;
var homer = new Person('homer');
homer.position = 'safety';
homer.getPosition = function() {
return this.position;
}
function Employee(first, position) {
this.first = first;
this.position = position;
}
Employee.prototype = homer;
var giselle = new Employee("Giselle", "hot model");
giselle.getName();
It seems to me that this last example is quite similar to the previous
example. This example is, in fact, more declarative than the previous
example because in this last example there are lines like
"Person.prototype = adam;" which says what it means quite clearly.
This example is also more efficient (although perhaps just slightly)
without the need for the "F() dance" in object(). This example uses
language-level constructs which which JavaScript programmers are
familiar. Even disregarding the object() function's length, this
example is shorter.
-------
Do you see advantages to using Crockford's object() function?
Do you see the apparent ugliness that Crockford seems to see in the
JavaScript built-in system of constructor functions, their prototype
objects and the "new" keyword?
Is there precedence for Crockford's object() function from another
language?
Should the JavaScript language have an facility like Crockford's?
object() function?
How would you code this Person-Employee example?
If you happen to be Douglas Crockford, how badly have I misinterpreted
your writing and speaking?
======================================================
REFERENCES
Crockford's page containing the object() function
<URL: http://javascript.crockford.com/prototypal.html>
Crockford's page mentioning parasitic inheritance
<URL: http://javascript.crockford.com/inheritance.html>
Crockford videos mentioning parasitic inheritance
See the "Advanced JavaScript" series on YUI blog
<URL: http://developer.yahoo.com/yui/theater/>
JSLint
<URL: http://jslint.com>
Crockford's Top Down Operator Precedence article
Precursor to his chapter in "Beautiful Code"?
<URL: http://javascript.crockford.com/tdop/tdop.html>