Z
Z
ZaxapKramer2016-11-19 18:12:58
JavaScript
ZaxapKramer, 2016-11-19 18:12:58

How to implement an object with methods that could be called sequentially?

How to implement a similar function

get.id("main").class("selector")[0].tag("span")[0];

instead of
document.getElementById("main").getElementsByClassName("selector")[0].getElementsByTagName("span")[0];

?
It seems to be simple, but I don’t fully understand how to do it somehow, but not call methods sequentially after (in this case) get . Thank you in advance for your help and I apologize for the "crooked" description of the problem .
get.id().get.class().get.tag()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
display: block, 2016-11-19
@ZaxapKramer

https://learn.javascript.ru/task/chain-calls

S
Sergey, 2016-11-19
Protko @Fesor

Perhaps you should elaborate on the example, since what you want is easier to do like this:

var el = document.querySelector('#id .selector span:first-of-type')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question