如何创建自定义的JS函数?

huangapple go评论67阅读模式
英文:

How can I make a custom JS function?

问题

这是我尝试做的一个想法 - 一个自定义的速记:

on_cl('#id'/*或者是.class而不是#id*/){
  //在这里插入代码
}

,不要一遍又一遍地粘贴这个:

document.querySelector('#id'/*或者是.class*/).addEventListener('click'() => {
  //在这里插入代码
})

基本上,这两段代码应该是等效的。

我该如何做到这一点?

我在MDNdocs上搜索了答案,但没有找到我想要的。

英文:

Here is an idea of what I'm trying to do - a custom shorthand:

on_cl('#id' /* or .class instead of #id */){
  // insert code here
}

, to not paste this over and over:

document.querySelector('#id' /* or .class */).addEventListener('click', () => {
  // insert code here
})

. Basically, these 2 chunks of code should be equivalent.

How can I do this?

I searched for an answer on MDNdocs, but didn't find what I wanted.

答案1

得分: 2

如果你想要为一个已经存在的方法创建一个快捷方式,你需要将新函数的参数传递给它,以便编写原始方法所需的一切。在你的情况下,你需要传递选择器和事件的回调函数,因此该函数应该如下所示:

function on_cl(selector, eventCallback){
  document.querySelector(selector).addEventListener('click', eventCallback)
}

然后像这样调用它:

on_cl('#id', () => {
  /// 你的回调函数
}); 

编辑

正如David Thomas在评论中建议的,还可以更改要监听的事件。

在函数on_cl中,唯一要监听的事件是"click"。通过将其作为函数的参数传递,可以更改它:

function on_event(selector, eventType, eventCallback){
  document.querySelector(selector).addEventListener(eventType, eventCallback)
}

on_event('#id', 'click', () => {
  /// 你的回调函数
}); 

如果需要的话,你可以为需要监听的每个事件创建多个快捷方式,像这样:

function on_click(selector, eventCallback){
  on_event(selector, "click", eventCallback);
}

function on_mouse_up(selector, eventCallback){
  on_event(selector, "mouseup", eventCallback);
}

// 等等...
英文:

If you want a shortcut for an already existing method, you will need to pass in the parameters of the new function everything you need to write the original method. In your case you would need the selector and the callback of the event, so the function should look like this :

function on_cl(selector, eventCallback){
  document.querySelector(selector).addEventListener('click', eventCallback)
}

And call it like this :

on_cl('#id', () => {
  /// your callback
}); 

Edit

As David Thomas suggested in the comments, it is also possible to change the event to listen to.

In the function on_cl, the only event to listen to is "click". This can be changed by passing it as an argument of the function

function on_event(selector, eventType, eventCallback){
  document.querySelector(selector).addEventListener(eventType, eventCallback)
}

on_event('#id', 'click', () => {
  /// your callback
}); 

If you want, you can then create multiple shortcuts for each event you need to listen to, like this :

function on_click(selector, eventCallback){
  on_event(selector, "click", eventCallback);
}

function on_mouse_up(selector, eventCallback){
  on_event(selector, "mouseup", eventCallback);
}

// etc...

huangapple
  • 本文由 发表于 2023年5月11日 18:45:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定