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
相关推荐
电子云与长程纠缠11 分钟前
UE5制作六边形包裹球体效果
开发语言·python·ue5
IT_陈寒16 分钟前
Vite动态导入把我坑惨了,原来要这样用才对
前端·人工智能·后端
砍材农夫17 分钟前
物联网 基于netty构建mqtt协议规范(遗嘱与保留消息)
java·开发语言·物联网·netty
DFT计算杂谈20 分钟前
KPROJ编译教程
java·前端·python·算法·conda
觅_22 分钟前
前端学习后端的时候 选择一个技术
前端·学习
独泪了无痕25 分钟前
CryptoJS:数据安全的JavaScript加密利器
前端·vue.js·node.js
froginwe1134 分钟前
Python3 迭代器与生成器
开发语言
xiaoshuaishuai81 小时前
C# 签名异常与Gas预估失败调试方案
开发语言·网络·tcp/ip·c#
xiaoshuaishuai81 小时前
C# Gemini 辅助网络安全漏洞分析
开发语言·web安全·c#
发现一只大呆瓜1 小时前
一文搞懂 Vite 处理CommonJS包、按需编译逻辑及 Rollup 插件兼容规则
前端