473,441 Members | 1,482 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,441 software developers and data experts.

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 1990
em*********@gmail.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:someValue, property2:someFunction };

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/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 10 '06 #2
em*********@gmail.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:someValue,
property2:someFunction
};

- 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*********@gmail.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:someValue,
property2:someFunction
};

- 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.sampleTranport = 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.hdlrFuncNames.length; x++){
var fn = dojo.io.hdlrFuncNames[x];
if(typeof kwArgs.handler == "object"){
if(typeof kwArgs.handler[fn] == "function"){
hdlrObj[fn] = kwArgs.handler[fn]||kwArgs.handler["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("load", 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.argsFromMap(kwArgs.content) ;
// sampleTransport.sendRequest(tgtURL, hdlrFunc);
}

dojo.io.transports.addTransport("sampleTranport");
}
*/

Aug 11 '06 #4

Lasse Reichstein Nielsen wrote:
em*********@gmail.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:someValue, property2:someFunction };

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/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

So is this essense an anonymous constructor function?

Aug 11 '06 #5
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrites:

[ 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.prototype

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/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 11 '06 #6
On 11/08/2006 02:01, em*********@gmail.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*********@gmail.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/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 11 '06 #8
em*********@gmail.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.sampleTranport = new function(){
this.canHandle = function(kwArgs){
<snip>
}
this.bind = function(kwArgs){
<snip>
}

dojo.io.transports.addTransport("sampleTranport");
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.sampleTranport = {
canHandle:function(kwArgs){
...
},
bind:function(kwArgs){
...
}
};
dojo.io.transports.addTransport("sampleTranport");

- 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*****@litotes.demon.co.ukwrites:

[ 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.prototype

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
em*********@gmail.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?
I don't like this use of new. I don't like implied invocations; I think that all
invocations should use the () suffix. Without it, people like Scott will be
confused. It also sets up an anonymous function as a constructor, which is weird
and a little wasteful.

I would rather see something like

object.property = function () {
...
return {
property: ...,
};
}();

This still allows the methods to be privileged.

http://javascript.crockford.com/
Aug 12 '06 #11

Douglas Crockford wrote:
em*********@gmail.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?

I don't like this use of new. I don't like implied invocations; I think that all
invocations should use the () suffix. Without it, people like Scott will be
confused. It also sets up an anonymous function as a constructor, which is weird
and a little wasteful.

I would rather see something like

object.property = function () {
...
return {
property: ...,
};
}();

This still allows the methods to be privileged.

http://javascript.crockford.com/
Thanks for the response. I have actually checked out you website before
and it's great. Hopefuly this is not bad edicate but I can send you an
email with a more eleborate example from Yahoo's API? I would really
like to get a good grasp of these concepts?

Thanks,

Scott

Aug 15 '06 #12
I would rather see something like
>
object.property = function () {
...
Yes, that makes a lot of sense I think.
Browsing through this issue on google, I came across this discussion
between some dojo developers:
http://dojotoolkit.org/pipermail/doj...ne/010675.html

One of them seem to be advocating a pattern like
var o = new function(){ ... }();
which looks like a mix-up of the two solutions discussed here. Or is
there a good motivation for using both "new" and "()" ?

Best regards
Mike

Sep 3 '06 #13
Oh, after actually thinking I realized that
var o = new function(){ ... };
and
var o = new function(){ ... }();
are equivalent.

Just like Lasse pointed out with named constructors earlier in the
thread:
var o = new Temp;
var o = new Temp();

Best regards
Mike

Sep 4 '06 #14
mi*****@hotmail.com wrote:
Oh, after actually thinking I realized that
var o = new function () { ... };
and
var o = new function () { ... }();
are equivalent.
So are

var o = new function () { ... }();

and

var o = function () { ... }();

except that the form with new creates a useless level of indirection.
There is no excuse for using new function.

http://javascript.crockford.com/
Sep 4 '06 #15

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

Similar topics

6
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...
1
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...
4
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...
15
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...
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...
12
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. ...
3
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? ...
4
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
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
1
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
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...
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,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.