简体   繁体   English

将“ this”传递给函数相当于什么?

[英]What's the equivalent of passing “this” to a function?

I have a function which is expecting "this" to be passed as a parameter but I would like to pass a different object without moving the trigger function, so currently I have this: 我有一个函数希望将“ this”作为参数传递,但是我想传递一个不同的对象而不移动触发函数,所以目前我有这样的函数:

onclick="showCalendarControl(this);"

but instead I would like to do this: 但是我想这样做:

onclick="showCalendarControl(document.getElementById('somethingelse'));"

This doesn't work as it stands, am I trying to do something impossible? 就目前情况而言,这是行不通的,我是否正在尝试做一些不可能的事情?

Try using the .bind() function. 尝试使用.bind()函数。 Docs 文件

The first argument to bind will set the functions' this argument (replacing the default one), all other arguments are translated into parameters once the method is called. bind的第一个参数将设置函数的this参数(替换默认参数),一旦调用该方法,所有其他参数都将转换为参数。

Examples: 例子:

onclick="showCalendarControl.bind(this)"  //the this-statement inside of showCalendarControl will be the specified one

onclick="showCalendarControl.bind(something, this);" //this will be passed as first argument to showCalendarControl

Edit: After testing it out, it seems that .bind() doesn't work in the onclick handler. 编辑:经过测试,似乎.bind()在onclick处理程序中不起作用。 But your initial idea worked, or at least what I understood as your problem (passing document.getElementById result to your method) 但是您的最初想法有效,或者至少是我所理解的问题(将document.getElementById结果传递给您的方法)

 function showCalendarControl(obj) { obj.innerHTML = "YEY"; } 
 <body> <div id="something" onclick="showCalendarControl(document.getElementById('somethingelse'));">Click Me</div> <div id="somethingelse"></div> </body> 

If you want to bind the parameter to the function's this statement, you can try this (use of the call() function instead of my initial bind): 如果要将参数绑定到函数的this语句,则可以尝试以下操作(使用call()函数代替我的初始绑定):

 function showCalendarControl() { this.innerHTML = "YEY"; } 
 <body> <div id="something" onclick="showCalendarControl.call(document.getElementById('somethingelse'));">Click Me</div> <div id="somethingelse"></div> </body> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript中jQuery的.before()函数的等价物是什么? - What is the equivalent of jQuery's .before() function in Javascript? 与iOS的委托函数等效的Javascript是什么? - What's Javascript equivalent to iOS's delegate function? emberjs组件中Angular的指令链接函数等效于什么? - What's the equivalent of Angular's directive link function in an emberjs component? 将参数传递给函数。 怎么了? - Passing argument to function. What's wrong? 在Recurly JS中更新订阅和帐户信息的功能等效于什么? - What's the equivalent of this function to update a subscription and account info in Recurly JS? 这个嵌套对象创建者函数的lodash等效项是什么? - What’s the lodash equivalent of this nested object creator function? jQuery在本机javascript中的功能之后和之后等效是什么? - What are the equivalent of after and before jQuery's function in native javascript? 在反应原生钩子中刷新 function 的拉动等效于什么? - What's the equivalent of this pull to refresh function in react native hooks? 在javascript中传递函数和函数调用本身之间的区别是什么? - What's the differenct between passing a function and the function call itself in javascript? 调用函数和传递函数有什么区别? - what's the difference between calling a function and passing a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM