SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?
    Code:
    	$('.rec_thumb').hover(
    	function(e){
    		var id = $(this).attr('id').replace('rec_thumb_', '');
    		switch_image(id);
    		e.preventDefault();
    	}, function() {
    		} );

  2. #2
    SitePoint Enthusiast Anthony.Barnes's Avatar
    Join Date
    Aug 2011
    Location
    Canberra, Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use call or apply to achieve something similar:

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

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 ?
    Code:
    function obj(){
    	this.calling = function(id) {
    		alert("Calling function, catched id: " + id );
    	};
    	
    	$('.a').hover(function() {
    		var id = 0;
    		obj.calling.call(obj, id); // <-- NOT WORKING!
    	} );
    	
    };
    var newobj = new obj();

  4. #4
    SitePoint Enthusiast Anthony.Barnes's Avatar
    Join Date
    Aug 2011
    Location
    Canberra, Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    http://jsfiddle.net/ZZVne/

  5. #5
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can try this method:
    HTML Code:
    <!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>

  6. #6
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok now i can access the function but the next problem is how to i access the variables?

    Code:
    var obj = function(name){
    	this.name = name;
    
       $('.a').hover(function() {
          obj.prototype.callit.call(obj, obj.name); // <-- Obj.name not returning any result!
       }, function() {} );
    };
    
    obj.prototype = {
        callit: function(name)
        {
            alert("Name: " + name);
        }
    };
    
    var newObj = new obj('Dan');


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •