How can invoke a function in Javascript automatically like jquery -


i've been trying understand how jquery works. let's instance when anchor tag clicked. take code below example:

$('.selector').click 

the "$" references jquery how jquery know supposed , find ('.selector') in dom? how jquery automatically know run following method of "click" on matched element?

$ function. you're passing ('.selector') argument. that's how knows fetch.

it returns jquery object populated dom elements found. jquery object has methods on it.

when call .click() on jquery object, iterates through matched dom elements it's holding, , performs expected operation.


here's very simple example.

var $ = function(selector) {     return new jquery(selector); }  function jquery(selector) {     var elems = document.queryselectorall(selector);     this.length = [].push.apply(this, elems); } jquery.prototype.click = function() {     (var = 0; < this.length; i++)         console.log("clicking", i, this[i].nodename);     return this; } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -