Javascript chaining

Hello everybody.

I am just starting with javascript and I am wondering how it should be done.
I mean… array of functions.

You dont have to be a genius to know that array could be defined like this:

var myfunction = function() {
     return {
         func1: function() {
         func2: function() {


This syntax allow us to use chaining like jQuery does.


But I tried to insert a function which hiding paragraph in this way and I can’t get a clue.

function show(node) {
  if(node && { = "block";

function hide(node) {
  if(node && { = "none";

They just won’t work in this way I presented up. Please guys I am asking for an advice.

Thanks in advance.

You don’t need 2 separate functions to show or hide an element.

You can do it using just a single function.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="">
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <style type="text/css"></style>
        <script type="text/javascript">
            function showHide(elemId){
                var obj = document.getElementById(elemId);
       = ( == 'none')? 'block' : 'none';
        <p id="myP">This is my paragraph</p>
            <button onclick="showHide('myP');">Show/Hide paragraph</button>

Yes I know.

That’s example functions. I just want to know how to implement these functions in this scheme, because I can’t get it to work.

ok, it isn’t clear in your op if the primary purpose is chaining itself or using chaining to show/hide an element.

Got any solution?

yeah, but I’m now helping someone else on another website. I’ll pop back later.

var funcs = function() {
	return {	
		$: function (id) {
			if(id.substr(0,1) == ".") { 
				return document.getElementsByClassName( id.substring(1) ).item(0);
			} else if(id.substr(0,1) == "#") {
				return document.getElementById( id.substring(1) ); 	
			return {
				hide: function () {
					if($(id) && $(id).style) {
						$(id).style.display = "none";
					return $(id);

function func() {

I want use it in this way


but it’s not working , whats wrong?


To be able to chain methods together in JavaScript each method needs to return a pointer to the object that the method belongs to. Then since the returned value is the same object as was originally referenced it can then have another method of that object chained to the end of it.

x = function() {
this.a = function() {alert('a'); return this;}
this.b = function() {alert('b'); return this;}
return this;
y = new x();

Thanks Mate.