Daniel wrote:
On Tue, 30 Jan 2007 23:11:18 +0000, Richard Cornford wrote:
>Daniel wrote:
>>Hi,
I was reading Douglas Crockford's article on prototypal inheritance
at http://javascript.crockford.com/prototypal.html. I think it
also relates to a post back in Dec 2005.
The mechanism discussed was:
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
Towards the end of the article, Douglas says that "For convenience,
we can create functions which will call the 'object' function for
us, and provide other customizations such as augmenting the new
objects with privileged functions. I sometimes call these maker
functions."
Any chance someone can give an example?
Something like:-
function objectPluss(o){
var obj = object(o);
var privateVar = '';
obj.getPrivateVar = function(){
return privateVar;
};
obj.setPrivateVar = function(v){
privateVar = v;
};
obj = null; // so no reference to the obj object is
// left hanging around in the closure.
}
Well, I responded to this message about a day ago using the
google groups service but despite the fact it said 'post
successful', I have yet to see it.
Yes, google don't appear to do QA, or they are not very good at it.
So to paraphrase myself (with a lot more brevity this time):
1) thanks for the above as demonstration of the possibility
2) why do you set null above at the point you do?
Generally you would not want to include more in a closure than was
necessary, and so if there was no need/intention to have the methods
reference their object instance through the scope chain there is no
reason to keep a reference to that object on the scope chain. That is
mostly to mitigate IE's memory leak problem when presented with circular
chains of references between JS and COM object (including DOM its nodes),
so the consideration does not apply when dealing with pure JS objects.
I would use objectPluss
to output the augmented object.
Yes, indeed the function is not much use unless it returns the new object
as there is no other way to reference it.
Which leads to:
3) is there a significant cost in using the objectPluss closure
multiple times to manufacture private variables and their access
functions as per the above.
That depends on what you call significant. Closures rely upon the unique
identity of function objects, and each call to the function will bring
two new functions objects into existence. In javascript the creation of
functions is not too heavyweight, but you still don't want to be creating
them when you don't need to.
There is a trade off, is the cost worth the benefits of having a
'private' storage area? And the answer to what depends on the context in
which you are doing it.
(I might also streamline objectPluss so it takes the
unaugmented object as an argument.)
That is more a style decision that anything else. My preference would be
to pass in the object that was to be augmented (after the use of your -
object - function) (and then there is no need to return the object from
the function as a reference to it would be available outside the
function). That also makes the function more general, as it can then be
used to augment any object with the interface it applies (including its
re-use in other code in other contexts).
I also noted in my other post (somewhere out there in
digital oblivion) that strictly speaking the variable
is effectively private and the function which can see
it is sort-of priveleged because I don't think it can
see existing private data formed in the constructor of
the object we are augmenting.
Each closure is as isolated from other closures as other code is isolated
from them.
I'm curious to see how much javascript with its simplicity
and functional approach can mimic a complex dynamic-oop
language like ruby.
Don't expect people who understand javascript to understand Ruby, any
more than you should expect people who understand Ruby to understand
javascript. If you want to ask comparative questions it is always a good
idea to fully explain the behaviour in the language that is not the
expertise of the people you ask the question of, because then the
probably will be able to answer the question properly rather than just
guessing.
Well, I'm going to hit the send button again - this time on
a legit news reader....
That worked, as expected.
Richard.