1

I mean this:

document.getElementById('Id')

With This:

function getById(ID){
return document.getElementById('Id')
}
getById('Id')

I am developing a project it requires using thousands of these... I'm using this with other DOM manipulation methods . Does it have any consequences?

  • 1
    No, that should not normally have any negative consequences. And you can do `var getById = document.getElementById;`, and achieve the same thing already. – CBroe Oct 13 '21 at 06:13
  • 1
    @CBroe That should be `const getById = document.getElementById.bind(document);`. See [JavaScript function aliasing doesn't seem to work](/q/1007340/4642212). – Sebastian Simon Oct 13 '21 at 06:14
  • 1
    Hi Javid ! beware to `return document.getElementById(ID);` ;) – Philippe Oct 13 '21 at 06:16
  • 3
    Consider _why_ “it requires using thousands of these”. Why do you need thousands of IDs? Are you using other means of DOM traversal like prototype methods of [`Element`](//developer.mozilla.org/docs/Web/API/Element#methods) and [`Node`](//developer.mozilla.org/docs/Web/API/Node#methods), [`querySelectorAll`](//developer.mozilla.org/docs/Web/API/Document/querySelectorAll), [event delegation](//developer.mozilla.org/docs/Learn/JavaScript/Building_blocks/Events#Event_delegation), [iteration](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#instance_methods), etc.? – Sebastian Simon Oct 13 '21 at 06:20
  • why not just use `document.querySelector('#Id')`? which is an all-in-one selector. [querySelector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) – ahsan Oct 13 '21 at 06:21

0 Answers0