English 中文(简体)
如何在一步骤中形成具有属性的职能目标
原标题:How to Create Function Object with attributes in one step

这是一个学术问题,是为了澄清我对 Java本的理解。

功能物体一旦产生即可被分配为属性。 然而,在一个步骤中能否形成具有属性的功能目标?

例如,

function someFunction(){
}

var someFunctionObject=someFunction;


someFunctionObject.attr1="attr 1";

alert(someFunctionObject.attr1); /* "attr 1"

在此,在提出标的后,第二步中正添加属性。 能否在一个步骤中做到这一点?

最佳回答

答案是没有的。 解释如下。

你的法典的最终结果如下:

var myFunctionObject = {
    prop1:  foo ,
    prop2:  bar ,
    construct: "alert( function body )"
};
myFunctionObject(); // should alert
myFunctionObject.prop1; //  foo 

功能定义/声明实际上非常相似,它产生一项标注,将[建筑]财产交给实际机构。 但是,这只是一个执行细节,你不能单独获取或确定[建筑]财产,因此,你无法在一戈内界定其他财产(这将需要额外的合成物,而且不存在)。

但是,如果符合你需要,你可以从职能范围内确定财产。 例如,这样做有助于打下:

function getSomething() {
    if (getSomething.prop) return getSomething.prop;
    return getSomething.prop = fetchResource();
}
问题回答

这样做的最佳方式是将一个指定的职能分配到如下变量:

var someFunctionObject = (function someFunction() {
    if (!someFunction.attr1) {
        someFunction.attr1 = "attr1";
        return someFunction;
    }
    // extra processing
}());

alert(someFunctionObject.attr1);

这增加了以下好处,即如果某人删除了 at1财产,则将随后重新开始:

delete someFunctionObject.attr1; // deleted
someFunctionObject();            // reinitialized
alert(someFunctionObject.attr1); // back to square one

撰写上述建筑的更好办法是:

var someFunctionObject = (function someFunction() {
    if (someFunction.attr1) {
        // extra processing
    } else {
        someFunction.attr1 = "attr1";
        return someFunction;
    }
}());

This is both faster and better because:

  1. We don t use the logical NOT operator. This saves an instruction to execute when interpreted. Logical NOT doesn t coerce, so using it has no benefits.
  2. The function object is usually only initialized once, so logically the initialization should come last as it ll be rarely called.
function someFunction(attr1){
  this.attr1 = attr1; 
}

var someFunctionObject = new someFunction( attr 1 );
console.log( someFunctionObject.attr1 ); 
var someFunc = new function(attr1){
  this.attr1 = attr1; 
}("blah");

alert(someFunc.attr1);

这是一个步骤,但不是一个非常有益的构造。

你在座右铭上只剩下了一点(但很重要)。 你在样本代码中所做的事情是发挥作用的,然后将这一功能指定为另一个物体上的财产(susactObject)。

What you did in literal terms looks like this:

var someFunctionObject = {
    someFunction : function(){},
    attr1 : "attr 1"
}

This results in one object (someFunctionObject, which is poorly named at this point, as it s just a regular object) with two properties assigned to it: one string, and one function.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签