473,750 Members | 2,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object declaration Question?

Below are some over simplified examples of code the create a single
instance of an object and I was hoping some one could give me the pros
and cons of each approach.

First declaration: In this case a single object is created using object
literal notation and both the get and __Private methods are availabile
for use and no closure is created.

ABC.Util.SomeOb ject =
{
get : function(val)
{
return this.__Private( val);
},

__Private : function(val)
{
return "testing";
}
}

Second declaration: In this case a single object is create and only the
get function is availabile for use and a closure is created.

ABC.Util.SomeOb ject = function()
{
var __Private : function(val)
{
return "testing";
}

return {
get : function(val)
{
return __Private(val);
}
};
}();

Oct 9 '06 #1
4 1476

em*********@gma il.com wrote:
Below are some over simplified examples of code the create a single
instance of an object and I was hoping some one could give me the pros
and cons of each approach.
That depends entirely on what you are trying to achieve. If you want
to mimic "private" methods, then your first example doesn't do that,
the second does.

First declaration: In this case a single object is created using object
literal notation and both the get and __Private methods are availabile
for use and no closure is created.

ABC.Util.SomeOb ject =
{
get : function(val)
{
return this.__Private( val);
},

__Private : function(val)
{
return "testing";
}
}

Second declaration: In this case a single object is create and only the
get function is availabile for use and a closure is created.

ABC.Util.SomeOb ject = function()
{
var __Private : function(val)
I think you mean to use an assignment here:

var __Private = function(val)
You can also use prototype-based inheritance:

function SomeObj(val)
{
this.x = val;
this._private = function(){
return this.x;
};
}

SomeObj.prototy pe.get = function()
{
return this._private() ;
}

var objA = new SomeObj('ObjA') ;
var objB = new SomeObj('ObjB') ;
alert( objA.get() + '\n' + objB.get() );
--
Rob

Oct 10 '06 #2
I guess what I'm trying to ask is when creating a single instance of an
object is there a downside to using closures? So in my first example a
closure is created giving ABC.Util.SomeOb ject access to the __Private
method. My understand is that when a closure is created the
Activation/Variable object is copied to the scope chain. Does this
happen for every invocation of the _get_ method?

Hopefully that makes sense.

Thanks,

Scott
RobG wrote:
em*********@gma il.com wrote:
Below are some over simplified examples of code the create a single
instance of an object and I was hoping some one could give me the pros
and cons of each approach.

That depends entirely on what you are trying to achieve. If you want
to mimic "private" methods, then your first example doesn't do that,
the second does.

First declaration: In this case a single object is created using object
literal notation and both the get and __Private methods are availabile
for use and no closure is created.

ABC.Util.SomeOb ject =
{
get : function(val)
{
return this.__Private( val);
},

__Private : function(val)
{
return "testing";
}
}

Second declaration: In this case a single object is create and only the
get function is availabile for use and a closure is created.

ABC.Util.SomeOb ject = function()
{
var __Private : function(val)

I think you mean to use an assignment here:

var __Private = function(val)
You can also use prototype-based inheritance:

function SomeObj(val)
{
this.x = val;
this._private = function(){
return this.x;
};
}

SomeObj.prototy pe.get = function()
{
return this._private() ;
}

var objA = new SomeObj('ObjA') ;
var objB = new SomeObj('ObjB') ;
alert( objA.get() + '\n' + objB.get() );
--
Rob
Oct 10 '06 #3
em*********@gma il.com wrote:
I guess what I'm trying to ask is when creating a single instance of an
object is there a downside to using closures?
Not really. The need to maintain the scope chain formed when creating
that closure will increase memory usage slightly, but it should be
negligible. If there are DOM objects referenced via that scope chain,
then there is the possibility of memory leaks in MSIE should those
references be circular, but that's not unusual and the remedy is the same.

The real question is if there are benefits to using a closure. If you're
happy with a convention to indicate private data (say, a leading
underscore), then that may be enough. On the other hand, if it would be
necessary to add properties to the global object (global variables and
functions), then it may be better to encapsulate those declarations
within the body of a function.
My understand is that when a closure is created the
Activation/Variable object is copied to the scope chain. Does this
happen for every invocation of the _get_ method?
I'm somewhat confused by what you're asking. As I see it, you're worried
that when a closure is created, extra run-time overhead is incurred when
that function is called. There isn't. The function is still called in
the usual way.

The scope chain (or more precisely, the internal [[Scope]] property) of
a function is determined when it is created. That is, when a function
declaration is parsed, or a function expression is evaluated (note the
subtle difference). For closures and inner functions, this will only
happen when the outer function is called - not at any other time.

ABC.Util.SomeOb ject = function() {
/* Note: there's no reason to use a function expression
* here.
*/
function private(val) {
return "testing";
}

return {
get : function(val) {
return private(val);
}
};
}();

During the execution of that enclosing function, the inner function
expression will be evaluated during evaluation of the object literal. It
is at this point that the [[Scope]] property for the get method is assigned.
The scope chain for any given execution context isn't actually
initialised until the function that establishes it is called. This is
because the scope chain includes the arguments passed to that function,
and these cannot be known until the call is made. At this point, the
activation object is added to the scope chain, followed by the objects
referenced by the [[Scope]] property. This occurs for any function,
whether in relation to a closure or not.

Hope that helps,
Mike

Please do not top-post when replying to this group. Moreover, please
trim quotes to include only relevant material.
Oct 11 '06 #4
Thanks for the help Michael.

Oct 12 '06 #5

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

Similar topics

1
7260
by: C. N. Sridhar | last post by:
Hi, I'm writing a wrapper to a win32 dll in C#. I need to call a method in DLL which has a Variant type reference parameter. How to marshal variant type from win32 (unmanaged code) to C# (managed code)? I tried using Marshal.GetObjectForNativeVariant(), but of no use.
44
2452
by: Steven T. Hatton | last post by:
This may seem like such a simple question, I should be embarrassed to ask it. The FAQ says an object is "A region of storage with associated semantics." OK, what exactly is meant by "associated semantics"? What, if any, associated semantics are shared by all objects? That part seems to go beyond the FAQ. Does anybody know of a resource that discusses (focuses on) this topic? -- p->m == (*p).m == p.m
19
2911
by: J. J. Farrell | last post by:
After many years of dealing with definition and linkage issues in ways that I know to be safe, I've decided it's time to try to understand this area properly. Consider a header file with the file scope declaration int i; This header is included in two files that refer to i but do not declare it. The two files build together into a single program.
16
3226
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for me. I was doing research on JavaScript inheritance recently (simplifying it in particular) and after reading 10.1.1, 10.1.3 and some other sections of ECMA262 I got a hint on accessing global object from different than global scope.
44
3379
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use it but but www.jslint.com is not happy with me.
36
3851
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same program will result in a linker error complaining about multiple definitions. So this kind of definition should be avoided as much as possible. But as we know, the definition of a class is always in a header file. And we can use "#ifndef" to eliminate...
5
1897
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject(); TheObject.Dictionary<string, string= new Dictionary<string, string>(); .... } The first line (TheObject instance = new TheObject();) doesn't get
9
2061
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will guarantee not to change the object?* In C++, as seen below, you can use the 'const' keyword in the function / method declaration. But how to do this in C#? *for example: "void Foo() const;"
11
23658
by: KMinFL | last post by:
This is a C# VS 2008 question... Our system has 2 base classes, SingleEntity and NewPluralEntity. SingleEntity provides access to properties and methods related to manipulating data in a database table and NewPluralEntity is a generic base class that I inherit from to create strongly typed collections of single entities. Here is the declaration of our NewPluralEntity class: public abstract class NewPluralEntity<T> : ICollection<T>,...
0
9001
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9583
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9256
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6808
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4888
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3323
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
2
2807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.