JS Rename Methods

I'm tired of writing and reading getElementByIdand getElementsByTagName... complicates programming for me.

So, I want to rename them. I want them to be getId, getTagsand getClasses.

This is the micro library I work for. How can I get this result with minimal CPU usage?

At least var doc = document;it was easy.

+4
source share
3 answers

Just assign them.

var doc = document;
doc.getId = doc.getElementById;
doc.getTags = doc.getElementsByTagName;
doc.getClasses = doc.getElementsByClassName;
alert(doc.getId('foo').className);
<div id="foo" class="classy"></div>
Run codeHide result

If you want to leave doc., you will need to define functions, for example:

function getId(id) {
    return document.getElementById(id);
}
+3
source

Javascript. . document, .

document.getId = document.getElementById; document.getTags = document.getElementsByTagName; document.getClasses = document.getElementsByClassName;

+1

Why not just use jQuery? getElementById becomes $.("#theID"). You can crop the "selector" with $.

0
source

Source: https://habr.com/ru/post/1627507/


All Articles