Function() or Variable ?

Discussion in 'JavaScript' started by piri00, Dec 7, 2009.

  1. #1
    Hi I am learning javascript and this is my script which works fine I just would like to get som advice for code, I can use variable number or Function number() with exactly same code and script will work.
    My question is what is right to use variable or function() or what is difference as both work same way?
    Thanks for help

    var number = Math.floor(Math.random() * 15)

    function number() {
    return Math.floor(Math.random() * 15)
    }


    Code with variable number:
    
    window.onload = newCard;
    
    function newCard() {
    	if (document.getElementById) {
    		for (var i=0; i<24; i++) {
    			setSquare(i);
    		}
    	}
    	else {
    		alert("Sorry, your browser doesn't support this script");
    	}
    }
    
    function setSquare(thisSquare) {
    	var currSquare = "square" + thisSquare;
    	var colPlace = new Array(0,1,2,3,4,0,1,2,3,4,0,1,3,4,0,1,2,3,4,0,1,2,3,4);
    	var colBasic = (colPlace[thisSquare] * 15);
    	var number = Math.floor(Math.random() * 15)
    	var newNum = colBasic + number + 1;
    
    	document.getElementById(currSquare).innerHTML = newNum;
    }
    
    
    
    HTML:
    code with function number()
    
    window.onload = newCard;
    
    function newCard() {
    	if (document.getElementById) {
    		for (var i=0; i<24; i++) {
    			setSquare(i);
    		}
    	}
    	else {
    		alert("Sorry, your browser doesn't support this script");
    	}
    }
    
    function setSquare(thisSquare) {
    	var currSquare = "square" + thisSquare;
    	var colPlace = new Array(0,1,2,3,4,0,1,2,3,4,0,1,3,4,0,1,2,3,4,0,1,2,3,4);
    	var colBasic = (colPlace[thisSquare] * 15);
    	var newNum = colBasic + number() + 1;
    
    	document.getElementById(currSquare).innerHTML = newNum;
    }
    
    
    function number() {
    	return Math.floor(Math.random() * 15)
    	}
    
    HTML:
     
    piri00, Dec 7, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    both are correct and better to use..

    Since this is small example and value for number or way the value is retrieved won't change much, keeping it as variable.

    Cases where value might be coming from different sources or after a complex logic, keeping a function makes sense to keep an abstraction layer for the user leaving job as easy as making a function call :)
     
    mastermunj, Dec 7, 2009 IP
  3. gacba

    gacba Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Use function() when you have something to calculate dynamically each time you access it. Use the variable when the value is just a piece of data to access.

    Mixing them is confusing for sure.
     
    gacba, Dec 7, 2009 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    look for patterns, things you tend to repeat often and can re-use over your application or site. once you identify such patters, bring them into a function and call the function instead.

    programming to the pattern is really a great way of thinking and can save you a lot of time in the long run. for instance, atm you are generating a random number based upon a max seed of 15. if you have that in body code or on many pages and you come to realise you need to increment this to 20, you should not have to find every instance where you use math.random and change it - you change the function number() instead and any code that relies upon it follows suit.

    i hope this makes sense.
     
    dimitar christoff, Dec 7, 2009 IP
  5. piri00

    piri00 Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks guys... :)
     
    piri00, Dec 8, 2009 IP
  6. automarketting

    automarketting Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i think both are good enough , use variable when value is some piece or a data supporting it.
     
    automarketting, Dec 8, 2009 IP