473,667 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

object.property = new function(){

Some of the object properties in the Dojo Toolkit are set to objects
but they are using syntax like this:

object.property = new function() {
this.property = someValue;
this.property = someFunction;
}

Is the property set to a new object and if so what is the "new
function()" statment doing?

Thanks,

Scott

Aug 10 '06 #1
14 2017
em*********@gma il.com writes:
object.property = new function() {
this.property = someValue;
this.property = someFunction;
}

Is the property set to a new object and if so what is the "new
function()" statment doing?
The "function ..." is simply a function expression, so the above
is equivalent to:
---
var Temp = function() {
this.property = someValue;
this.property = someFunction;
}
object.property = new Temp
---
where "new Temp" is the same as "new Temp()".

So, yes, object.property is assigned a new object as value,
and that object is initialized using the following function.
If the anonymous initializer function is as simple as in this
example, it would be simpler to use an object literal, i.e.,

object.property = { property:someVa lue, property2:someF unction };

Even for more elaborate computations, you can do them first and
then create an object literal from the resulting values.

Compared to using "new" on an anonymous function, the object literal
approach does not create a prototype object referring to the anonymous
function, so it would be more memory efficient, but probably not
as readable

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 10 '06 #2
em*********@gma il.com wrote:
Some of the object properties in the Dojo Toolkit are set
to objects but they are using syntax like this:

object.property = new function() {
this.property = someValue;
this.property = someFunction;
}

Is the property set to a new object and if so what is
the "new function()" statment doing?
The (right side) operand of the - new - operator is expected to evaluate
as a reference to a function object, and that function object is used to
construct a new object that is the result of the - new - expression (an
arguments list is optional with the - new - operator and omitted here).

The the right of the new operator is a function _Expression_, which is
evaluated when executed and results in a function object being created,
with the value of the function expression being a reference to that new
(anonymous, in this case) function object.

The two together are a completely legal way of creating a single unique
object, but completely pointless (particularly in terms of the needless
complexity of the expression). When a function is used as a constructor
its use allows for the inheritance of methods/properties from a
prototype, but here no such inheritance is possible because there is no
method of accessing the prototype of the anonymous function object (it
goes out of scope as soon as the assignment expression is finished).

A simpler alternative would be:-

object.property = {
property:someVa lue,
property2:someF unction
};

- and the effect would be the same (but probably much more efficiently).

I would question the depth of understanding of javascript possessed by
someone who wrote your original version in preference to the above
(Which doesn't bode well for the Dojo Toolkit). However, there may be
closure-based code within the real (anonymous) constructor functions
which may justify their use.

Richard.
Aug 10 '06 #3

Richard Cornford wrote:
em*********@gma il.com wrote:
Some of the object properties in the Dojo Toolkit are set
to objects but they are using syntax like this:

object.property = new function() {
this.property = someValue;
this.property = someFunction;
}

Is the property set to a new object and if so what is
the "new function()" statment doing?

The (right side) operand of the - new - operator is expected to evaluate
as a reference to a function object, and that function object is used to
construct a new object that is the result of the - new - expression (an
arguments list is optional with the - new - operator and omitted here).

The the right of the new operator is a function _Expression_, which is
evaluated when executed and results in a function object being created,
with the value of the function expression being a reference to that new
(anonymous, in this case) function object.

The two together are a completely legal way of creating a single unique
object, but completely pointless (particularly in terms of the needless
complexity of the expression). When a function is used as a constructor
its use allows for the inheritance of methods/properties from a
prototype, but here no such inheritance is possible because there is no
method of accessing the prototype of the anonymous function object (it
goes out of scope as soon as the assignment expression is finished).

A simpler alternative would be:-

object.property = {
property:someVa lue,
property2:someF unction
};

- and the effect would be the same (but probably much more efficiently).

I would question the depth of understanding of javascript possessed by
someone who wrote your original version in preference to the above
(Which doesn't bode well for the Dojo Toolkit). However, there may be
closure-based code within the real (anonymous) constructor functions
which may justify their use.

Richard.

Here is the actual code from the toolkit (which I hope is not a problem
since it's open source). So if I understand correctly sampleTransport
becomes a new object but instead of using an object literal they used
the "new function()" statement. Is there a benefit to doing this?

/*
dojo.io.sampleT ranport = new function(){
this.canHandle = function(kwArgs ){
// canHandle just tells dojo.io.bind() if this is a good transport to
// use for the particular type of request.
if(
(
(kwArgs["mimetype"] == "text/plain") ||
(kwArgs["mimetype"] == "text/html") ||
(kwArgs["mimetype"] == "text/javascript")
)&&(
(kwArgs["method"] == "get") ||
( (kwArgs["method"] == "post") && (!kwArgs["formNode"]) )
)
){
return true;
}

return false;
}

this.bind = function(kwArgs ){
var hdlrObj = {};

// set up a handler object
for(var x=0; x<dojo.io.hdlrF uncNames.length ; x++){
var fn = dojo.io.hdlrFun cNames[x];
if(typeof kwArgs.handler == "object"){
if(typeof kwArgs.handler[fn] == "function") {
hdlrObj[fn] = kwArgs.handler[fn]||kwArgs.handle r["handle"];
}
}else if(typeof kwArgs[fn] == "function") {
hdlrObj[fn] = kwArgs[fn];
}else{
hdlrObj[fn] = kwArgs["handle"]||function(){};
}
}

// build a handler function that calls back to the handler obj
var hdlrFunc = function(evt){
if(evt.type == "onload"){
hdlrObj.load("l oad", evt.data, evt);
}else if(evt.type == "onerr"){
var errObj = new dojo.io.Error(" sampleTransport Error: "+evt.msg);
hdlrObj.error(" error", errObj);
}
}

// the sample transport would attach the hdlrFunc() when sending the
// request down the pipe at this point
var tgtURL = kwArgs.url+"?"+ dojo.io.argsFro mMap(kwArgs.con tent);
// sampleTransport .sendRequest(tg tURL, hdlrFunc);
}

dojo.io.transpo rts.addTranspor t("sampleTranpo rt");
}
*/

Aug 11 '06 #4

Lasse Reichstein Nielsen wrote:
em*********@gma il.com writes:
object.property = new function() {
this.property = someValue;
this.property = someFunction;
}

Is the property set to a new object and if so what is the "new
function()" statment doing?

The "function ..." is simply a function expression, so the above
is equivalent to:
---
var Temp = function() {
this.property = someValue;
this.property = someFunction;
}
object.property = new Temp
---
where "new Temp" is the same as "new Temp()".

So, yes, object.property is assigned a new object as value,
and that object is initialized using the following function.
If the anonymous initializer function is as simple as in this
example, it would be simpler to use an object literal, i.e.,

object.property = { property:someVa lue, property2:someF unction };

Even for more elaborate computations, you can do them first and
then create an object literal from the resulting values.

Compared to using "new" on an anonymous function, the object literal
approach does not create a prototype object referring to the anonymous
function, so it would be more memory efficient, but probably not
as readable

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'

So is this essense an anonymous constructor function?

Aug 11 '06 #5
"Richard Cornford" <Ri*****@litote s.demon.co.ukwr ites:

[ object.property = new function(){...} ]
When a function is used as a constructor its use allows for the
inheritance of methods/properties from a prototype, but here no such
inheritance is possible because there is no method of accessing the
prototype of the anonymous function object (it goes out of scope as
soon as the assignment expression is finished).
Actually, and probably unintended, it is available, as:
object.property .constructor.pr ototype

That leaves both the function and its prototype hanging around for
no good reason.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 11 '06 #6
On 11/08/2006 02:01, em*********@gma il.com wrote:
Richard Cornford wrote:
[snip]
>I would question the depth of understanding of javascript possessed
by someone who wrote your original version in preference to the
above (Which doesn't bode well for the Dojo Toolkit). However,
there may be closure-based code within the real (anonymous)
constructor functions which may justify their use.

So if I understand correctly sampleTransport becomes a new object but
instead of using an object literal they used the "new function()"
statement.
That's correct.
Is there a benefit to doing this?
As Richard noted, the latter approach is useful in closure-based code
where the execution context formed by the function expression becomes
the basis for that closure. However, this is not the case for the code
you posted: the canHandle and bind properties could have been added
within an object literal, and the addTransport method call could have
been made at any point after the object was assigned to the
sampleTranport [sic] property.

There are two other issues, both within the bind method:
}else{
hdlrObj[fn] = kwArgs["handle"]||function(){};
This assignment statement is executed within a loop. Whenever the
function expression here is evaluated (if the handle property of kwArgs,
type-converted to Boolean, evaluates to false), a new function object
will be created with its [[scope]] property containing the entire
execution context stack. Needless to say, this is quite pointless for a
function that, when called, does nothing. It would have been better to
define a dummy function elsewhere and assign a reference to that, thus
adding less overhead.
var hdlrFunc = function(evt){
The second issue is much simpler: why the above isn't simply a function
declaration, I don't know. Unless the bind method errors out, the above
will always be evaluated, and the time of evaluation is irrelevant in
this instance. I think the authors were getting a little carried away,
using function expressions just for the hell of it.

Mike
By the way, please don't post code containing tabs: they wrap far too
easily. Use a couple of spaces per level instead.
Aug 11 '06 #7
em*********@gma il.com writes:
Here is the actual code from the toolkit (which I hope is not a problem
since it's open source). So if I understand correctly sampleTransport
becomes a new object but instead of using an object literal they used
the "new function()" statement. Is there a benefit to doing this?
In this particular case, none.

Generally, probably none too. If you do not need the extra layer of a
prototype for the constructed object, there is no reason to create
it.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 11 '06 #8
em*********@gma il.com wrote:
Richard Cornford wrote:
<snip>
>I would question the depth of understanding of javascript
possessed by someone who wrote your original version in
preference to the above (Which doesn't bode well for the
Dojo Toolkit). However, there may be closure-based code
within the real (anonymous) constructor functions which
may justify their use.

Here is the actual code from the toolkit (which I hope is not
a problem since it's open source). So if I understand
correctly sampleTransport becomes a new object but instead
of using an object literal they used the "new function()"
statement. Is there a benefit to doing this?

/*
dojo.io.sampleT ranport = new function(){
this.canHandle = function(kwArgs ){
<snip>
}
this.bind = function(kwArgs ){
<snip>
}

dojo.io.transpo rts.addTranspor t("sampleTranpo rt");
This is the only line of code that actually gets executed as a
consequence of calling the anonymous function object as a constructor.
}
*/
There does not appear to be any reason in this code for not using an
object literal:-

dojo.io.sampleT ranport = {
canHandle:funct ion(kwArgs){
...
},
bind:function(k wArgs){
...
}
};
dojo.io.transpo rts.addTranspor t("sampleTranpo rt");

- as the closures formed at the constructor level in the original are
not (and so represent another avoidable overhead).

Richard.

Aug 11 '06 #9
Lasse Reichstein Nielsen wrote:
"Richard Cornford" <Ri*****@litote s.demon.co.ukwr ites:

[ object.property = new function(){...} ]
>When a function is used as a constructor its use allows for
the inheritance of methods/properties from a prototype, but
here no such inheritance is possible because there is no
method of accessing the prototype of the anonymous function
object (it goes out of scope as soon as the assignment
expression is finished).

Actually, and probably unintended, it is available, as:
object.property .constructor.pr ototype

That leaves both the function and its prototype hanging
around for no good reason.
That must be the second or third time that construct has slipped my
mind, probably because it is far too convoluted to ever consider using
it.

Richard.
Aug 11 '06 #10

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

Similar topics

6
2331
by: Luke | last post by:
Here is my emails to Danny Goodman (but probably he is very busy so he didn't answered it). First email(simple): Subject: JavaScript Arrays " We all know the array can act like HashMap, but is there a shortcut for creating such array ? eg //simple array indexed via numbers var a = new Array("a", "fdsf", "sada"); or,
1
9249
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex DataBinding accepts as a data source either an IList or an IListSource at System.Windows.Forms.ListControl.set_DataSource(Object value)
4
1870
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in browsers) ? What about the new syntax of creating a object using { 'propName1':'propValue1', 'propName2':'propValue2', 'propName3':{ /* another object */ } } - from what version of JScript/JavaScript it was supported (from what browsers versions) ?...
15
1899
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following two? (1)
26
5669
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
12
5539
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
3
2764
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Defining_Functions doesn't actually give any insight.
4
2118
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
2
2646
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
23
2625
Frinavale
by: Frinavale | last post by:
JavaScript is a very strange place for me... So, I decided that, before I attempt to create an Object, I should first learn about Objects. I had actually created one before with the help of a tutorial. The tutorial was complicated because showed me how to create a JavaScript Object that is a representation my .NET Object and how to get the two to "work together as one".... apparently the tutorial was not very clear about what was going on in...
0
8458
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
8366
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
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8565
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7391
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
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
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2017
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.