访问函数内的参数名称字符串
Access argument name string inside the function
是否可以访问参数名称字符串?!
function myFunction (a, b, c, d, e, f) {
var obj = [];
[].forEach.call(arguments, function(arg) {
obj.push({
// question is how to get variable name here?
name: "a",// "a", "b", "c", "d", "e", "f"
value: arg, //a, b, c, ,d, e, f
})
});
return obj;
}
myFunction(1,2,3,4,5,6); // return [{name: "a", value: 1}, {name: "b", value: 2}...]
注意:我知道使用 arguments
不是一个好习惯。我想知道这是否可能?
你可以尝试这样的事情:
function myFunction (a, blabla, c, somethingElse, e, f) {
var obj = [];
//'(a, b, c, d, e, f)'
var tmp = arguments.callee.toString().match(/'(.*?')/)[0];
//["a", "b", "c", "d", "e", "f"]
var argumentNames = tmp.replace(/[()'s]/g,'').split(',');
[].splice.call(arguments,0).forEach(function(arg,i) {
obj.push({
// question is how to get variable name here?
name: argumentNames[i],
value: arg
})
});
return obj;
}
console.log(JSON.stringify(myFunction(1, 2, 3, 4, 5, 6)));
//Output-> [{"name":"a","value":1},{"name":"blabla","value":2},
// {"name":"c","value":3},{"name":"somethingElse","value":4},
// {"name":"e","value":5},{"name":"f","value":6}]
演示
函数参数名称是否如上文所述在函数定义中定义?如果是这样,您可以在函数中创建一个数组:
function myFunction(a,b,c,d,e,f) {
var argNames = ['a','b','c','d','e','f'];
...
}
arguments
将只包含传递给函数的参数的简单数组。没有从参数到参数名称的默认映射。反正我知道
这有效:
function myFunction( _obj ) {
var obj = [],
names = [],
args = arguments,
len = function(o) {
var a = [];
for (var i in o) a.push(i);
return a;
};
for (var i in _obj) names.push( i );
[].forEach.call( len(_obj), function(a, b) {
obj.push({
name: names[b],
value: args[0][names[b]]
});
});
return obj;
}
console.log( myFunction({
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: 6
}));
这是一个演示
为了完整起见:你不能在javascript中使用命名参数。如果要使用它们(从而能够识别参数名称),解决方法是提供一个对象:
function myParams(params){
var ret = [];
for (var l in params){
if (params.hasOwnProperty(l)){
ret.push(l);
}
}
return ret
}
myParams({a:1,b:2,c:3,d:null,e:null,f:null,g:11});
//=> [a,b,c,d,e,f,g]
另请参阅...
并且要真正完整:也看到这个
您可以在函数上调用 toString,然后使用某种正则表达式来解析结果。
function someFunction(a,b,c){}
var functionString = someFunction.toString(); //return "function someFunction(a,b,c){}"
var args = functionString.match(/^function (?:.*?)'((.*?)')/);
if(args[1])
{
var argsNames = args[1].split(",");
}
我已经在FF和Chrome上测试过它。
由于 item.product_image2 是一个 URL 字符串,因此当您在参数中调用 changeImage 时,您需要将其放在引号中。
我的函数点击
items+='<img src='+item.product_image1+' id="saleDetailDivGetImg">';
items+="<img src="+item.product_image2+" onclick='changeImage('""+item.product_image2+"'");'>";
我的函数
<script type="text/javascript">
function changeImage(img)
{
document.getElementById("saleDetailDivGetImg").src=img;
alert(img);
}
</script>
相关文章:
- 从查询字符串参数推断出正确的数据类型
- 如何使用JQuery在HTML中创建包含字符串参数的引号的onclickjavascript链接
- 如何在 OnClick 函数中传递字符串参数
- 使用Angular Resource查询具有字符串参数的REST API
- 如何在JavaScript函数中将字符串参数传递给Java方法
- 为javascript函数提供可能包含特殊字符的字符串参数
- 使用查询字符串参数筛选数据
- 将查询字符串参数与谷歌地图api v3服务结合使用
- 从 URL - AngularJS 获取动态查询字符串参数
- 函数中的 Javascript 字符串参数
- 将服务字符串参数转换为数组的 Angular 内部
- 将字符串参数传递给 JavaScript 函数
- 无法通过 ajax 将字符串参数传递给 php 函数
- 使用基于查询字符串参数的 Angular 填充选择元素
- 从标记中的 href 获取查询字符串参数
- 从带有字符串参数的函数中的元素中获取值
- 将字符串参数传递给 javascript 函数 mvc
- 将查询字符串参数添加到 GridView 项模板
- 将字符串参数从 JS 视图传递到控制器
- 使用 RegEx 进行 JavaScript 字符串参数编码