简体   繁体   English

如何不通过争论仍然使功能工作?

[英]How to not pass arguements and still make the function work?

function drawLine(ctx, sX, sY, eX, eY, sRGB, fRGB, lWidth, capStyle)
{
    ctx.beginPath();
    ctx.moveTo(sX, sY);
    ctx.lineTo(eX, eY);
    ctx.lineWidth = lWidth||5;
    ctx.strokeStyle = 'rgb(49, 129, 48)';
    ctx.lineCap = 'round';
    ctx.stroke();
    ctx.closePath();
}

And then I want to call the function like this: 然后我想调用这样的函数:

drawLine(ctx, 50, 50, 100, 100, someStrokeStyle, someFillStyle, someCapStyle);

As you can see I have skipped the lWidth parameter. 如您所见,我跳过了lWidth参数。 I want the function to still work, even when the lWidth is not passed as a parameter. 我希望函数仍然有效,即使lWidth没有作为参数传递。 How will I do this? 我该怎么做? Atm, it might think that the someCapStyle is the lwidth . Atm,它可能会认为someCapStylelwidth

When you have a big amount of arguments to pass into a function like you have, use an object: 当你有大量的参数传递给你的函数时,使用一个对象:

function foo({param1: val1, parma2: val2}) {}

In that case you wont be depend on number of arguments and order of them being represented. 在这种情况下,你不会依赖于参数的数量和它们的表示顺序。

So you can rewrite your function: 所以你可以重写你的功能:

 function drawLine(drawObj)
{
    ctx.beginPath();
    ctx.moveTo(drawObj.sX, drawObj.sY);
    ctx.lineTo(drawObj.eX, drawObj.eY);
    ctx.lineWidth = drawObj.lWidth||5;
    ctx.strokeStyle = drawObj.sRGB;
    ctx.lineCap = drawObj.capStyle;
    ctx.stroke();
    ctx.closePath();
}

When you don't pass any argument, undefined value is passed instead, so just check in the function whether the argument has been passed or not: 当你没有传递任何参数时,会传递undefined值,所以只需检查函数是否已经传递参数:

if(typeof argument == "undefined") 
{ 
   argument = "default value";
}

So to not pass lWidth , just pass undefined as its value 因此,为了不传递lWidth ,只需将undefined作为其值传递

PS the best way is to use a single argument args , which will be object containing all current parameters as properties. PS最好的方法是使用单个参数args ,它将包含所有当前参数作为属性的对象。

What you want is to partially evaluate the drawLine function, assigning a constant value to lWidth . 你想要的是部分评估drawLine函数,为lWidth分配一个常量值。 There's a JavaScript library called Jeene that does just this. 有一个名为Jeene的JavaScript库就是这样做的。 This is how you would use it: 这是你如何使用它:

function drawLine(ctx, sX, sY, eX, eY, sRGB, fRGB, lWidth, capStyle) {
    ctx.beginPath();
    ctx.moveTo(sX, sY);
    ctx.lineTo(eX, eY);
    ctx.lineWidth = lWidth || 5;
    ctx.strokeStyle = "rgb(49, 129, 48)";
    ctx.lineCap = "round";
    ctx.stroke();
    ctx.closePath();
}

Function.prototype.specialize = net.higherorder.jeene.Jeene.make();

var drawLine2 = drawLine.specialize({
    lWidth: null // or whatever value you want
});

Then you use drawLine2 as follows: 然后使用drawLine2 ,如下所示:

drawLine2(ctx, 50, 50, 100, 100, someStrokeStyle, someFillStyle, someCapStyle);

This is called specialization and is a very useful pattern. 这称为特化,是一种非常有用的模式。 Read more about it: A Neighborhood of Infinity: The Three Projections of Doctor Futamura 阅读更多相关信息: 无限邻居:Futamura医生的三个预测

You can put the optional parameter at the end of the paramter list. 您可以将可选参数放在参数列表的末尾。 That way, if you leave it out, the other parameters won't be affected. 这样,如果你把它留下,其他参数不会受到影响。

Another option would be to pass a single object with the attributes you want to define eg 另一种选择是传递具有您想要定义的属性的单个对象,例如

function drawLine(options) {
    options.ctx.beginPath();
    options.ctx.moveTo(options.sX, options.sY);
    options.ctx.lineTo(options.eX, options.eY);
    // etc.
 }

You can not use "function overloading" in Javascript, but here is a way to achieve what you want: 你不能在Javascript中使用“函数重载”,但这是一种实现你想要的方法:

How to overload functions in javascript? 如何在javascript中重载函数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM