English 中文(简体)
绕过 com子的争执
原标题:Pass comma-separated arguments to js function
  • 时间:2010-12-09 16:06:03
  •  标签:
  • javascript

我有以下职能:

function calculateAspect(options){
    def = {
        或gw: 0,
        或gh: 0,
        tarw: 0,
        tarh: 0
    };
    o = $.extend(def,options);
    console.log(o);


};    
calculateAspect({或gw:640});

我愿通过以下价值观:

calculatedAspect(640,480,320)

calculateAspect(200)

考虑到这一职能,这似乎不合逻辑。 但是,我很奇怪的是,它是如何撤出的。

最佳回答

您可使用https://developer.mozilla.org/en/Javagust/Reference/Functions_and_Function_cop/arguments” rel=“nofollow”arguments,其中载有所有经通过的论点:

function calculateAspect(options){
    var argNames = ["orgw","orgh","tarw","tarh"];
    def = {
        orgw: 0,
        orgh: 0,
        tarw: 0,
        tarh: 0
    };
    for (var i=0, n=Math.min(arguments.length, 4); i<n; i++) {
        def[argNames[i]] = arguments[i];
    }
    console.log(o);
}
问题回答

您必须就参数的含义制定自己的公约。

function calculateAspect(orgw, orgh, tarw, tarh) {
  var def = { /* ... */ };
  if (arguments.length === 1 && (typeof orgw) === "object") {
    var o = $.extend(def, orgw);
    // ... normal code ...
  }
  else {
    calculateAspect({orgw: orgw, orgh: orgh, tarw: tarw, tarh: tarh});
  }
}

可能的话。





相关问题
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.

热门标签