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 分钟前
Rust String 与 &str 详解:字符串所有权、借用与转换
前端·后端·rust
码农学院13 分钟前
GEO与SEO协同:从传统搜索到生成式搜索的平滑迁移路径
服务器·前端·python
To_OC8 小时前
LC 131 分割回文串:刚学回溯时,我连怎么切字符串都想不明白
javascript·算法·leetcode
咩咩啃树皮9 小时前
第40篇:Vue3组件化开发精讲——组件拆分、复用、父子通信、工程化架构
java·前端·架构
阳光是sunny9 小时前
LangGraph中的Reducer是什么
前端·人工智能·后端
触底反弹9 小时前
一文搞懂 Tailwind CSS 弹性布局:从原理到实战
前端·css·html
灯澜忆梦9 小时前
GO_并发编程---定时器
开发语言·后端·golang
阳光是sunny9 小时前
从链到图:LangGraph 入门基础全解析
前端·人工智能·后端
-银雾鸢尾-9 小时前
C#中的StringBuilder相关方法
开发语言·c#
To_OC9 小时前
LC 42 接雨水:暴力超时卡半天?前后缀数组一用就通了
javascript·算法·leetcode