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
相关推荐
傻乐u兔9 小时前
C语言进阶————指针4
c语言·开发语言
大模型玩家七七9 小时前
基于语义切分 vs 基于结构切分的实际差异
java·开发语言·数据库·安全·batch
历程里程碑9 小时前
Linux22 文件系统
linux·运维·c语言·开发语言·数据结构·c++·算法
恋猫de小郭9 小时前
Flutter Zero 是什么?它的出现有什么意义?为什么你需要了解下?
android·前端·flutter
牛奔10 小时前
Go 如何避免频繁抢占?
开发语言·后端·golang
寻星探路14 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
崔庆才丨静觅15 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
lly20240615 小时前
Bootstrap 警告框
开发语言
2601_9491465316 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧16 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言