English 中文(简体)
Javascript: 通过多重论点作为单一变量
原标题:Javascript: passing multiple arguments as a single variable

是否能够利用单一变量通过多个论点? 例如,如果我想要做这样的事情:

function foo(x,y){
    document.write("X is " + x);
    document.write("Y is " + y);
}

var bar = "0,10";
foo(bar);

以上的例子就是我试图做些什么的简化例子。 它不做工作(因为“禁运”被作为单一理由被发现)。 我知道,使用这些阵列的方法比较容易。

因此,我问这个问题主要是出于好奇,能否找到“禁运”变量,以作为非一种情况,而只是两个论点?

感谢!

问题回答
function foo(thing) {
    document.write("X is " + thing.x);
    document.write("Y is " + thing.y);
}

var bar = {x:0, y:10};
foo(bar);

你要求的是不可能的。 如果你想通过单一论点中的多重价值,则使用<代码>Array或。 目标。 如果你真的must 各位得打脚,请打电话split(),以打破将论点分为一阵。

function Add (a, b, c) {
    return a + b + c;
}

var nums = [1, 2, 4];
var sum = Add.apply (null, nums);

变式论据清单:

function Add () {
    var sum = 0;
    for (var i = 0; i < arguments.length; i++) {
        sum += arguments[i];
    }
    return sum;
}
var n = Add (1, 2, 3, 4, 5);

参考:applymeth (Function Object)

通常的做法是通过备选办法的标语。

function foo(options){
  //...
}

那么,你可以做任何事情。

var opts = {};//create an object
opts[ x ] = 5;//set whatever properties you want
opts[ y ] = 23;
opts[ border ] = 3;
foo(opts);//pass 1 argument, with as many values as you want

通常,这些数值是线性界定的,特别是如果这些数值不需要在方法之外。

foo({ x :5, y :23, border :3});

实际情况并非如此。

你可以做到:

window.foo.apply(window, bar.split( , ));

(请你通过一系列论点,而不是单独提出每一论点)

......但“有点”一语是铭记的。

您可使用:

var bar = [0,10]; // creates an array
foo(bar);

function foo(arg){
    document.write("X is " + arg[0]);
    document.write("Y is " + arg[1]);
}

页: 1

function foo(options){
    document.write("X is " + options.x);
    document.write("Y is " + options.y);
}

var bar = {x: 0, y:10};

无,是不可能的。 你可以提出两个论点,但一个阵列仍然是一个变数。 然后,你们需要重新履行接受一个变量的职能,并把它当作一个阵列来对待。

function foo(x){
document.write("X is " + x[0]);
document.write("Y is " + x[1]);
}

基本上,一项职能接受各种变量作为论据,无论你通过哪一种变数,每个变数仍只有一个变数——无法找到一个单一变数作为多重论据加以确认。 一个阵列是一个变数,一个小目标是一个变数,等等。 这些物品有多个部分,但以单一变量重新排列。

如何? (ES6+)

function foo({x, y}){
    document.write("X is " + x);
    document.write("Y is " + y);
}

并称:

foo({x:10, y:5})

There is a downside to using a single structured argument over multiple arguments, and that is with multiple arguments you can use /** in may IDEs to generate a method header which will display an @param for each argument. But if you only have one argument then you will lose the niceness of a description for each argument and hence less useful intelli-sense in the IDE as it wont pick up the docuemntation of the structure s properties.

/**
 * Do stuff
 * @param {*} param0 - A structure containing the blah, blah blah data
 */
function foo({x, y}){

而不是。

/**
 * 
 * @param {*} x - The value for blah
 * @param {*} y - the value for blah-blah
 */
foo1(x, y){

页: 1 值得注意的是,您的<条码> /条码>界定了它只具有一种价值,其中含有“0,10”的缩略语。

function myFunction(a,b){
//do stuff with a and b here
}

myFunction(1, text )

......

<a onClick="myFunction(1, text );"

http://www.quirksmode.org/js/Function.html“rel=“nofollow”>here





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