object augmentation and priveleged functions

D

Daniel

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?

By 'priveleged' I want a function that can access the private
variables in the object. eg

function F() {
var private_var = "this is private";
this.get_private_var = function() { return private_var; }
}

So, this amounts to being able to augment f with 'get_private_var'
after doing 'f = new F()' , instead of defining get_private_var within
the scope of the F constructor function as per the above fragment.

Many Regds,
Daniel
 
R

Richard Cornford

Daniel said:
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.

}

Richard.
 
D

dd

Any chance someone can give an example?

By 'priveleged' I want a function that can access the private
variables in the object. eg

function F() {
var private_var = "this is private";
this.get_private_var = function() { return private_var; }
}

You might find it easier to follow if you watch his video
lectures here: developer.yahoo.com/yui/theater/

There you'll find seven videos (4 regular, 3 advanced). Even
the most advanced people here I'm sure will learn something.
I learned quite a few things from them. Anyone who thinks
they can skip the first 4, because they're already advanced,
will be missing some good stuff. Watch 'em all if you plan to
do anything substantial with JS - they're worth it.
 
D

Daniel

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.

}

Richard.

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.

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? I would use objectPluss
to output the augmented object.

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. (I might also streamline objectPluss so it takes the
unaugmented object as an argument.)


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. I'm curious to see how much javascript with
its simplicity and functional approach can mimic a complex dynamic-oop
language like ruby.

Well, I'm going to hit the send button again - this time on a legit news
reader....

Cheers,
Daniel

Thanks again,
Daniel
 
R

Richard Cornford

Daniel said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top