JavaScript Avoid declaring variables on scroll? avoid declaring public instance variables

var variableOne;
var variableTwo;
var variableThree;

function veryInterestingFunction() {

  var variableFour;

  if(a){
    variableFour = 'Good';
  }
  else {
    variableFour = 'Decent';
  }

  console.log(variableFour);

}

$(window).on('scroll', function(){

  veryInterestingFunction();

});

Here I have some variables in the global scope, a function declaring a variable and assigning a value to it, and calling this function on scroll. This way on every scroll you are going to declare the "variableFour" which is not a good practice, right? However I don't want to crowd the global scope with unnecessary variables and also can't use an IIFE. Is there a "best practice" way to declare the variable outside the function and still only possible to use it inside the scope of that function ?

http://jsfiddle.net/2jyddwwx/1/

Answer:1

to declare the variable outside the function and still only possible to use it inside the scope of that function ?

I guess, that's impossible

When I don't want to crowd the global scope, I will declare one global object, for example App = {}, and instead of global variables, I use it's properties.

App.variableOne;
App.variableTwo;
App.variableThree;

Or you can use classes in ES6

Answer:2

I don't think there's anything wrong with your code sample, I seriously doubt variable declaration is ever going to slow down your code. I'd definitely only start to worry about this kind of thing when you're absolutely certain it's causing issues (it won't), otherwise it might be wasted effort.

If you really want to optimize this, one thing you might be able to do is debouncing the veryInterestingFunction calls - that is if you don't necessarily need to respond to every single scroll event (depends on your logic).

As per your question, IIFE/function closure is essentially the only way to limit scope in JavaScript that I know of, so if you can't use that, I don't see any other option.

Answer:3

I'm having a table where the user will enter data in the fields when they enter each value in each cell, the last cell needs to be updated with its total. with this snippet I'm getting 45+90+36. I ...

I'm having a table where the user will enter data in the fields when they enter each value in each cell, the last cell needs to be updated with its total. with this snippet I'm getting 45+90+36. I ...

  1. angularjs get table values

I'd like to reduce my array of objects using lodash. My object: [ {date: '01/01/2016', price: 5}, {date: '01/01/2016', price: 10}, {date: '01/02/2016', price: 500} ] After the reduce: [...

I'd like to reduce my array of objects using lodash. My object: [ {date: '01/01/2016', price: 5}, {date: '01/01/2016', price: 10}, {date: '01/02/2016', price: 500} ] After the reduce: [...

  1. reduce array with one element
  2. reduce array with objects
  3. array_reduce with keys
  4. reduce with array
  5. array reduce with index
  6. array reduce with initial value
  7. reduce with array javascript
  8. array reduce with promise
  9. array reduce with condition
  10. array reduce with strings
  11. array reduce with keys php
  12. reduce with array ruby
  13. array reduce with examples
  14. reduce array of objects javascript
  15. reduce array of objects js
  16. reduce array of objects into one object
  17. reduce array to unique values
  18. reduce array to string javascript
  19. reduce array by adding elements
  20. reduce array of booleans

I'm new to web crawling. I am trying to crawl a webpage using java and I encounter a problem. I need to get the link in a 'HTML Tag' whose href is a javascript function. I have no idea how to get the ...

I'm new to web crawling. I am trying to crawl a webpage using java and I encounter a problem. I need to get the link in a 'HTML Tag' whose href is a javascript function. I have no idea how to get the ...

Hi I have created a dom using raw javascript DOM: Javascript code: var parentElm = document.createElement("div"); var child1 = document.createElement("p"); var child2 = document.createElement("p"); ...

Hi I have created a dom using raw javascript DOM: Javascript code: var parentElm = document.createElement("div"); var child1 = document.createElement("p"); var child2 = document.createElement("p"); ...