I'd like to add an event listener to a field, such as this
field.addEventListener("keyup", (event) => {
if (event.keyCode === 13) {
event.preventDefault();
// do something
}
});
Is there a clean way to keep event
and to destructure keyCode
from it, because I need both?
You can destructure the object in a new const variable.
field.addEventListener("keyup", (event) => {
const { keyCode } = event;
if (keyCode === 13) {
event.preventDefault();
// do something
}
});
You could destructure to object, in the parameter list, the object is no gone, but you could pull it from the arguments
object .
Which does not make the code nicer ...
function perform({ baz }) { console.log(baz); console.log(arguments[0]); } var object = { foo: 'bar', baz: 42 }; perform(object);
You could something like,
const use = (i, cb) => cb(i, i)
use(doSomething(), (event, { keyCode }) => {
//
});
Well, unfortunately, no, not the way I think you mean. You could do something like this:
field.addEventListener("keyup", event => {
// whatever you want to destructure
const {keyCode} = event;
if (keyCode === 13) {
event.preventDefault();
// do something
}
});
This probably isn't that helpful, though, as I'm sure you want to destructure in the parameter list of your event handler.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.