adi rohan adi rohan - 5 months ago 15
jQuery Question

Add methods to function

I want to add method to a function such that i can call them in a chain

What i want to do:

If you have used jQuery , then you must have seen the syntax like

$("#ID");


returns element

$("#ID").css("property")


returns css value of property of element

How can i do it by defining custom methods with same syntax without using jQuery ?

Answer

Here is a simple one:

function Get( element_id ) {
    this.node = document.querySelector(element_id);
    this.css = function(prop, val) {};
}

var get = function( element_id )
{
    return new Get( element_id );
};

get('#ID').css("property");