Reputation: 15299
I heard that, the variables all are need to declare on the top of the function by standards.
But in case when the element is created in between the function, and another element depends the width or height of the creating element(dynamicaly), how we can declare the variables at the top?
this is my function:
var stepSlider = function(holder,column){
$.each(holder, function(i,value) {
var container = $(value),
colCount = column,
boardViewer = container.find('.stepRangeCompound'),
boardHolder = container.find('.boardHolder'),
board = container.find('.indBoard'),
boardCount = board.size(),
boardWidth = board.outerWidth(true),
boardHeight = board.outerHeight(true),
initial=0;
//setting sizes
while(initial < boardCount){
if(initial % colCount === 0){
$('<div />',{
class:'boardColumn',
css:{
float:'left',
height:boardHeight*colCount
}
})
.appendTo(boardHolder) //after append only i am getting the width and element at all.
}
$('.boardColumn:last').append(board[initial]);
initial++;
}
var colWidth = $('.boardColumn').width(), //i am declaring variable here
coloSize = $('.boardColumn').size();
boardViewer.css({
width:colCount*colWidth // i am applying the values here.
});
boardHolder.css({
width:coloSize * colWidth // i am applying the values here.
});
})
}
if i am wrong any one guide me into correct way to declare all variables only on top?
Upvotes: 0
Views: 108
Reputation: 9131
you can declare a variable on a fly in JavaScript. NO need to declare it. but even then you want to declare all used variable on top. you can declare them with null values ans then assign them the value you want
var colWidth,coloSize ; // on the top you declare
.
.
.
colWidth = $('.boardColumn').width(); // in the middle you assign them the value
coloSize = $('.boardColumn').size();
Upvotes: 2
Reputation: 26719
You can declare them at the top, with null values, and assign values when the element is created
Upvotes: 1