简体   繁体   English

可以使用ColdFusion语法在JavaScript中引用参数吗?

[英]Can arguments be referenced in JavaScript using ColdFusion syntax?

In ColdFusion, if you want to reference 在ColdFusion中,如果要参考

<cfargument name="x">

then you say: 然后你说:

arguments.x

In JavaScript, if you have a function: 在JavaScript中,如果您有一个函数:

var myFunction = function(x) {

then, is there a way to explicitly reference the arguments scope like maybe: 然后,有没有办法像下面这样显式引用参数范围:

arguments[0].x

or something so that you're scoping everything. 或其他东西,以便您确定所有范围。

There is no way to achieve the same functionality by using the arguments variable, as it holds no information on parameter names. 通过使用arguments变量无法实现相同的功能,因为它不包含有关参数名称的信息。 To circumvent this, you could switch from using multiple parameters to one compound parameter object that holds actual parameter values in its members. 为了避免这种情况,您可以从使用多个参数切换到一个在其成员中保留实际参数值的复合参数对象。

<script>
   function abc(params) {
    var x = params.x;
     var y = params["y"];
  }

   abc( { x: 10, y: "hello" });
</script>

This way however you lose some of the readability of the code at the function signature, plus you must provide param names on the calling side. 但是,这样一来,您将失去函数签名处的代码的某些可读性,此外,您还必须在调用方提供参数名称。

You can reference the arguments pseudo-variable, but the arguments are indexed by number, not by name. 您可以引用arguments伪变量,但是参数是按数字而不是按名称索引的。 It's a good idea to avoid messing with arguments directly; 避免直接弄乱arguments是个好主意。 a common idiom is to convert it to a real array: 一个常见的习惯用法是将其转换为实数组:

var args = Array.slice.call(arguments, 0);

I'm afraid not. 恐怕不是。 You can explicitly use x or arguments[0] but nothing more. 您可以显式使用xarguments[0]但仅此而已。 Unless, as pointed out from others, you pass an object. 除非别人指出,否则您将传递对象。

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

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