Javascript - 将参数添加到作为参数传递的函数中 [英] Javascript - add parameters to a function passed as a parameter

查看:108
本文介绍了Javascript - 将参数添加到作为参数传递的函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function one(func){
func(5 );


函数two(arg1,arg2){
console.log(arg1);
console.log(arg2);


一(二(3))//打印3,5

这种行为或类似的东西可以在javascript中完成吗?

解决方案

有些解决方法是可行的。 b
$ b

function one(){var args = Array.prototype.slice.call(arguments); var func = args [0]; args.splice(0,1); args.push(5); func.apply(this,args);} function two(arg1,arg2){console.log(arg1); console.log(arg2);} one(two,3)

Here is the behaviour I'm looking for:

function one(func){
   func(5);
}

function two(arg1, arg2){
   console.log(arg1);
   console.log(arg2);
}

one(two(3)) //prints 3, 5

Can this behaviour or something similar be accomplished in javascript?

解决方案

Some workaround is possible

function one() {

  var args = Array.prototype.slice.call(arguments);
  var func = args[0];
  args.splice(0, 1);
  args.push(5);
  func.apply(this, args);
}

function two(arg1, arg2) {
  console.log(arg1);
  console.log(arg2);
}

one(two, 3)

这篇关于Javascript - 将参数添加到作为参数传递的函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆