Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Tiffany McAllister
Tiffany McAllister
25,806 Points

JQuery Code Improvements/Suggestions

Hey everyone,

I've been wanting to learn a bit more about JavaScript/JQuery lately so I've decided to do it by creating some small projects. One of these projects is an image gallery. It's all working and doing what I want it to do, but I was just wondering if anyone could give me any suggestions to improve the code. Is there easier/better ways to do things, etc?

Any comments are appreciated!

http://codepen.io/anon/pen/Gyjns

Hi, Tiffany McAllister:

I recommend an object-oriented approach of creating, setting, and instantiating (starting) your gallery.

That usually looks like this (ES6 JS or CoffeeScript)

# I'm using CoffeeScript for the sake of simplicity first 
class Gallery
  constructor: (@element, @initialPage, @options)
    # iniitalize code 

The JavaScript equivalent today is the following:

function Gallery(element, iniitaPage, options){
  this.el = element; 
  this.initialPage = initialPage; 
  this.options = options 

}

I also recommend having a namespace to avoid the high chance of someone else using the same variables as you do when it comes to their code only for your code to override their variables using the same, basic names and the other way around.

1 Answer

Tiffany McAllister
Tiffany McAllister
25,806 Points

Thanks Kevin! I'm not too familiar with OOP. I've been trying to learn but find it pretty confusing. I will have to give it another go :)