-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Moved all id, classname and jquery selector strings to "constants" objects #1066
Conversation
… "constants" objects
I can see it useful mainly in terms of compression, but the reason of making it easier to change doesn't convince me. It is almost as easy as using a "find an replace" and I don't see this variables as something that will be changed with frequency. In any case, I would use real constants instead of objects, this way you could save more then 2KB (10%) in the minification and the syntax will be shorter and more easy to read. |
I think extracting these as constants is much more than just for compression (though that alone is a very valid argument imo). Not having "magic" strings all over the code helps with maintainability, refactoring, lessens the risk of typos/regressions, and could help contributers (and with taking contributions) as all the DOM selectors are now easily glance-able. Though do agree that using raw vars (consts) would help the minification much more. |
@eamodio yeah. I agree with you. Although I believe some of those strings shouldn't be constants, such as:
Having combinations of selectors which are only used in specific cases, such as the Then, not quite sure about the other combinations such as |
@alvarotrigo Definitely agree with combining the constants rather than having combined constants, unless the combination really defines something that should be uniquely addressable (and used repeatedly). |
@alvarotrigo @eamodio - agree with individual constant variables and also separating combo selectors where used sparingly. For selectors like 'body' and 'html, body', I think those should just be declared once and as jQuery objects since those shouldn't need to be re-looked up in the DOM. |
Ok. Fine then. Should we just name the constants with upper cases without any prefix suffix to make them shorter and easier to read while coding? For example: What about the class names? Should we just use a function for it? function getClass(selector){
return selector.substr(1)
}
$(SECTION).hasClass(getClass(ACTIVE_SLIDE)); What do you think about it? |
Using CAPS for the constants sounds good. But I would argue against using a function for conversion from selector to class or vice versa. My main argument would be around performance and the amount of strings that would get allocated and destroyed, which would put more pressure on the garbage collector. Causing more collections could cause more pauses in the experience. I would suggest to just use un-prefixed CAPS variables for classes and prefixed (or post-fixed) CAPS variables for selectors that are the combination of the class variables.
or
I believe that would play well for readability, minification, and performance. Thoughts? |
Yeah, sounds reasonable. @d13 if you can update to the latest versoin 2.6.0 and make another pull request based on this comments I'll merge it. |
Will do! |
Conflicts: jquery.fullPage.js jquery.fullPage.min.js
@alvarotrigo this should be good to go |
Missing replacement of |
The line 870 fix was done on commit 9702c05. SLIDE_PREV and SLIDE_WRAPPER_SEL references fixed. |
Thanks for it! |
Moved all id, classname and jquery selector strings to "constants" objects
I though it would compress the strings, but it seems it doesn't :D Check out this example: http://jsfiddle.net/4d1jd1rk/ YUI compressor: var i="active";var h=".fp-section";d(h).addClass(i);.... Closure compiler: a(".fp-section").addClass("active");a(".fp-section").addClass("active");.... |
I use UglifyJS2 (https://github.com/mishoo/UglifyJS2) for compression and it seems to do it. |
Interesting: From the FAQs:
|
- Changed some variable names
- Change version to force bower update
This helps with future maintenance of the library as changing a classname or selector will only require a change in one place instead of many.