object.property = new function(){ [英] object.property = new function(){

查看:51
本文介绍了object.property = new function(){的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dojo Toolkit中的一些对象属性设置为对象

,但它们使用如下语法:


object.property = new function (){

this.property = someValue;

this.property = someFunction;

}


是否将属性设置为新对象,如果是,那么new

function()是什么?声明在做什么?


谢谢,


Scott

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

推荐答案

em*********@gmail.com 写道:
em*********@gmail.com writes:

object.property = new function(){

this.property = someValue;

this.property = someFunction ;

}


属性设置为新对象,如果是,那么什么是new

函数() "在做什么?
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?



功能......只是一个函数表达式,所以上面的

相当于:

---

var Temp = function(){

this.property = someValue;

this.property = someFunction;

}

object.property = new Temp

---

其中new Temp与new Temp()相同。


所以,是的,object.property被赋予一个新对象作为值,

和那个使用以下函数初始化对象。

如果匿名初始化函数的功能与此

示例一样简单,则使用对象文字会更简单,即


object.property = {property:someValue,property2:someFunction};


即使是更复杂的计算,你也可以先做,

然后根据结果值创建一个对象文字。


与使用new相比在一个匿名函数上,对象文字

方法不会创建一个引用匿名

函数的原型对象,所以它会更有内存效率,但可能不是

可读


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

DHTML死亡颜色:< URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> ;

''没有判断的信仰只会降低神灵的精神。''

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.''


em ********* @ gmail.com 写道:

一些对象Dojo Toolkit中的属性设置为对象的
,但它们使用如下语法:


object.property = new function(){

this.property = someValue;

this.property = someFunction;

}


属性设置为一个新的对象,如果是,那么什么是

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?



- new - 运算符的(右侧)操作数预计将评估

作为对函数对象的引用,并且函数对象用于构造一个新对象,它是 - new - 表达式的结果(

参数列表是可选的 - new - 运算符,在这里省略)。

new运算符的右边是一个函数_Expression_,它在执行时被评估为
,导致函数对象被创建,

,函数表达式的值是对新

(在本例中为匿名)函数对象的引用。


两个在一起是一个完全合法的方式来创建一个独特的
对象,但完全没有意义(特别是在表达式的不必要的复杂性方面)。当一个函数被用作构造函数时,它的使用允许继承来自

原型的方法/属性,但是这里没有这样的继承是可能的,因为没有

访问匿名函数对象原型的方法(一旦赋值表达式完成,它就会超出范围)。


更简单的替代方案是: -


object.property = {

property:someValue,

property2:someFunction

};


- 效果会相同(但可能效率更高)。


我会质疑对javascript所拥有的javascript的深度理解

有人编写你的原始版本而不是上面的

(这对于Dojo Toolkit来说并不是好兆头)。但是,在真实(匿名)构造函数

中可能有基于闭包的代码

这可能证明它们的使用是合理的。


Richard。

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.




Richard Cornford写道:

Richard Cornford wrote:
em ********* @ gmail.com 写道:

Dojo Toolkit中的一些对象属性设置为对象但是它们使用如下语法:


object.property = new function() {

this.property = someValue;

this.property = someFunction;

}


属性是否设置为新对象,如果是,那么

是什么?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?



- new - 运算符的(右侧)操作数应该评估

作为对函数对象的引用,并且函数对象用于构造一个新对象,它是 - new - 表达式的结果(

参数列表是可选的 - new - 运算符,在这里省略)。

new运算符的右边是一个函数_Expression_,它在执行时被评估为
,导致函数对象被创建,

,函数表达式的值是对新

(在本例中为匿名)函数对象的引用。


两个在一起是一个完全合法的方式来创建一个独特的
对象,但完全没有意义(特别是在表达式的不必要的复杂性方面)。当一个函数被用作构造函数时,它的使用允许继承来自

原型的方法/属性,但是这里没有这样的继承是可能的,因为没有

访问匿名函数对象原型的方法(一旦赋值表达式完成,它就会超出范围)。


更简单的替代方案是: -


object.property = {

property:someValue,

property2:someFunction

};


- 效果会相同(但可能效率更高)。


我会质疑对javascript所拥有的javascript的深度理解

有人编写你的原始版本而不是上面的

(这对于Dojo Toolkit来说并不是好兆头)。但是,在真实(匿名)构造函数

中可能有基于闭包的代码

这可能证明它们的使用是合理的。

$ b $理查德。


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.



这是工具包中的实际代码(我希望这不是问题

,因为它是开源的)。因此,如果我理解正确的sampleTransport

成为一个新对象,而不是使用对象文字他们使用

new function()声明。这样做有什么好处吗?


/ *

dojo.io.sampleTranport = new function(){

this .canHandle = function(kwArgs){

// canHandle告诉dojo.io.bind()如果这是一个很好的运输到

//用于特定类型请求。

if(



(kwArgs [" mimetype"] ==" text / plain")||

(kwArgs [" mimetype"] ==" text / html")||

(kwArgs [" mimetype"] ==" text / javascript" )

)&&(

(kwArgs [" method"] ==" get")||

(( kwArgs [" method"] ==" post")&&(!kwArgs [" formNode"]))



){

返回true;

}


返回false;

}


this.bind = function(kwArgs){

var hdlrObj = {};


//设置一个处理程序对象

for(var x = 0; x< dojo.io.hdlrFuncN ames.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"] ;

}

}否则if(typeof kwArgs [fn] ==" function"){

hdlrObj [fn] = kwArgs [ fn];

} else {

hdlrObj [fn] = kwArgs [" handle"] || function(){};

} $ / $
}


//构建一个回调给处理程序的处理函数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);

}

}


//示例传输将附加hdlrFunc()在此时向管道发送

//请求时

var tgtURL = kwArgs.url +"?" + dojo.io.argsFromMap(kwArgs.content) );

// sampleTransport.sendRequest(tgtURL,hdlrFunc);

}


dojo.io.transports.addTransport( " sampleTranport");

}

* /


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


这篇关于object.property = new function(){的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆