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
相关推荐
沐知全栈开发2 分钟前
JavaScript 计时事件
开发语言
Yang-Never2 分钟前
Android 应用启动 -> Android 多种方式启动同一进程,Application.onCreate() 会多次执行吗?
android·java·开发语言·kotlin·android studio
plmm烟酒僧2 分钟前
《微信小程序demo开发》第一部分-编写页面逻辑
javascript·微信小程序·小程序·html·微信开发者工具·小程序开发
期待のcode3 分钟前
Java 共享变量的内存可见性问题
java·开发语言
Monly214 分钟前
【大前端】前期准备-Trae开发工具安装
前端
会游泳的石头4 分钟前
深入剖析 Java 长连接:SSE 与 WebSocket 的实战陷阱与优化策略
java·开发语言·websocket
yutian06064 分钟前
TI-C2000 系列 TMS320F2837X 控制律加速器(CLA)应用
开发语言·ti·ti c2000
2601_949720269 分钟前
flutter_for_openharmony手语学习app实战+学习进度实现
javascript·学习·flutter
夕阳之后的黑夜13 分钟前
Python脚本:为PDF批量添加水印
开发语言·python·pdf
lllljz15 分钟前
blenderGIS出现too large extent错误
java·服务器·前端