473,586 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_privat e_var = function() { return private_var; }
}

So, this amounts to being able to augment f with 'get_private_va r'
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 1572
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.getPrivateV ar = function(){

return privateVar;

};

obj.setPrivateV ar = 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...@gmai l.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_privat e_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.getPrivateV ar = function(){

return privateVar;

};

obj.setPrivateV ar = 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.getPrivateV ar = function(){
return privateVar;
};
obj.setPrivateV ar = 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
22505
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 instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was...
8
1738
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 telling me that the script is "unresponsive", often more than once. I'm not sure if it's simply execution time or memory usage. My question is...
26
5659
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 several parts of the DOM, but this does not include the window object. Thank you
14
2793
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 shows all the properties, weeks available, pricing, etc. for a particular area of Europe. The data varies widely depending on the region; at times...
0
7911
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.