-1
Code:
this.boxes.forEach(box => document.body.removeChild(box) );
In ES5 it would be:
this.boxes.forEach(function (box) {
try {
return document.body.removeChild(box);
} catch(e){
//erros
}
});
ES6 Arrow Function file:
hideResizers() {
// stop listening for image deletion or movement
document.removeEventListener('keyup', this.checkImage);
this.quill.root.removeEventListener('input', this.checkImage);
// reset user-select
this.setUserSelect('');
this.setCursor('');
// remove boxes
this.boxes.forEach(box => document.body.removeChild(box) );
// release memory
this.dragBox = undefined;
this.dragStartX = undefined;
this.preDragWidth = undefined;
this.boxes = [];
}
How it was compiled:
function hideResizers() {
// stop listening for image deletion or movement
document.removeEventListener('keyup', this.checkImage);
this.quill.root.removeEventListener('input', this.checkImage);
// reset user-select
this.setUserSelect('');
this.setCursor('');
// remove boxes
this.boxes.forEach(function (box) {
return document.body.removeChild(box);
});
// release memory
this.dragBox = undefined;
this.dragStartX = undefined;
this.preDragWidth = undefined;
this.boxes = [];
}
The mistake:
I see no profit in using the Arrow Function of ES6 in this case. Why not keep the
function
?– Woss
Because sometimes there’s a mistake in the gift... I wanted to know how to do too, just for learning.
– Ivan Ferrer
I wanted to see something like, I don’t know;
this.boxes.forEach(box => try => catch { document.body.removeChild(box) } => e);
– Ivan Ferrer
I didn’t quite understand the question. If you’re doing a foreach means that boxes are an existing array, so why check the code
document.body.removeChild(box)
will return error?– Sam
If you already know the mistake happens when
box
is not the typeNode
, And you want to write that code faster, because it doesn’tthis.boxes.forEach(box => box instanceof Node && document.body.removeChild(box) )
?– Andre