bind函数--修改this指向,返回一个函数

1、bind函数--修改this指向,返回一个函数

2、返回一个函数,这个函数继续增加参数,会放在其后

javascript 复制代码
// 测试 concatMethods 参数接收情况
function concatMethods(...methods) {
  console.log('实际接收到的参数数量:', methods.length);
  console.log('参数类型:', methods.map(m => typeof m));
  let res = false;
  for (const method of methods) {
    const isBoolean = (typeof method === "function" ? method() : method);
    console.log('方法执行结果:', isBoolean);
    res = res || isBoolean;
    if (isBoolean) {
      break;
    }
  }
  return res;
}

// 模拟你的代码场景
const btnDisabled = () => {
  console.log('btnDisabled 执行了');
  return false;
};

const screenControl_Disabled = (device) => {
  console.log('screenControl_Disabled 执行了,设备参数:', device);
  return true;
};

const DEVICE_SUPPORT = { res: 'test-device' };

// 模拟 bind 调用
const boundFunc = concatMethods.bind(
  this, 
  btnDisabled, 
  screenControl_Disabled.bind(null, DEVICE_SUPPORT.res)
);

console.log('=== 开始执行 ===');
const result = boundFunc(true, 6666, "ka");
console.log('最终结果:', result);

// === 开始执行 ===
// 实际接收到的参数数量: 5
// 参数类型: [ 'function', 'function', 'boolean', 'number', 'string' ]
// btnDisabled 执行了
// 方法执行结果: false
// screenControl_Disabled 执行了,设备参数: test-device
// 方法执行结果: true
// 最终结果: true
相关推荐
古茗前端团队8 小时前
急招!前端|测试|后端|产品(名额多,速来)
前端·后端·架构
Lsx_8 小时前
不只是 Prompt:用 Superpowers Skill 给 AI 编程装上工程化工作流
前端·ai编程·claude
用户938515635079 小时前
从 Prompt 到 Harness:AI 工程化的三年跃迁与实战解码
javascript·人工智能
小碗细面9 小时前
前端 Prompt 工程实战:如何搭建场景化 Prompt 库
前端·ai编程
阿瑞IT9 小时前
2026年 AI Agent 生产化落地全景:四大高频故障根因分析与工程解法
前端
木木剑光9 小时前
我开源了一个 React 组件库,沉淀了多个高频组件和实用 Hooks
前端·javascript·react.js
kyriewen9 小时前
DeepSeek API 高峰时段涨价 2 倍,便宜大碗的时代要结束了?
前端·ai编程·deepseek
Moment9 小时前
牛逼,NextJs 从 16.3 开始全面拥抱 Agent Native 🥰🥰🥰
前端·后端·面试
沸点小助手10 小时前
6月沸点活动获奖名单公示|本周互动话题上新🎊
前端·后端
Csvn10 小时前
React 19 `use()` 来了:以后数据加载可以不用 useEffect?
前端·react.js