Is there static functions in javascript objects which can be called without instance?

This is my problem this code is wrapped in a object function
and i need to call the “switch image” which is on the gallery class

in php i could declare a functio as static and go to it like that
gallery::switch_image(id);

how do i do that on javascript?


	$('.rec_thumb').hover(
	function(e){
		var id = $(this).attr('id').replace('rec_thumb_', '');
		[B]switch_image(id);[/B]
		e.preventDefault();
	}, function() {
		} );

You can use call or apply to achieve something similar:


// call
gallery.switch_image.call(gallery,id);

// or apply which takes an array of arguments
gallery.switch_image.call(gallery,[id,..]);

i tried this way and its NOT working :\
why?

I have to access to “obj” object calling method and i cant find a way to do it…
anyone knows what do i need to do ?


function obj(){
	this.calling = function(id) {
		alert("Calling function, catched id: " + id );
	};
	
	$('.a').hover(function() {
		var id = 0;
		obj.calling.call(obj, id); [COLOR="#FF0000"]// <-- NOT WORKING![/COLOR]
	} );
	
};
var newobj = new obj();

I don’t quite get why are you doing certain things in that example, like instantiating a new object when you want to use a function statically. What you can do if you want to use a function statically, or as a method on newly created objects you can use the objects prototype:

You can try this method:


<!DOCTYPE html>
<html>
	<body>
		<a href="#" id="rec_thumb_test1">Link 1</a>
		<a href="#" id="rec_thumb_test2">Link 2</a>		
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
		<script>
			var Gallery = (function () {
				function _switch(id) {
					alert(id);
				}
				return {
					'switch_image': _switch
				};
			}());
			
			$('a').click(function(e) {
				e.preventDefault();
				var id = $(this).attr('id').substr(10);
					 $(this).attr('id', id);
				Gallery.switch_image(id);
			});
		</script>
	</body>
</html>

ok now i can access the function but the next problem is how to i access the variables?


var obj = function(name){
	this.name = name;

   $('.a').hover(function() {
      obj.prototype.callit.call(obj, obj.name); [B][COLOR="#FF0000"]// <-- Obj.name not returning any result![/COLOR][/B]
   }, function() {} );
};

obj.prototype = {
    callit: function(name)
    {
        alert("Name: " + name);
    }
};

var newObj = new obj('Dan');