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");
}
*/