473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

object augmentation and priveleged functions

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

Jan 30 '07 #1
4 1560
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.

}

Richard.

Jan 30 '07 #2
dd
On Jan 30, 1:52 pm, "Daniel" <dlb.id...@gmail.comwrote:
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.

Jan 31 '07 #3
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.

}

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
Feb 3 '07 #4
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.

Feb 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
8
by: Gawain Lavers | last post by:
I have a script which uses a lot of object augmentation (in order to extend the functionality of DOM elements), and I'm clearly stressing out my browser (I rapidly get to a point where Firefox is...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
14
by: julie.siebel | last post by:
I've been wrestling with a really complex page. All the data is drawn down via SQL, the page is built via VBScript, and then controlled through javascript. It's a page for a travel company that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.