Friday, 17 January 2014

private in javascript

var CONFIG = (function() { var privates = { 'MY_CONST': '1', 'ANOTHER_CONST': '2' }; return { get: function(name) { return privates[name]; } }; })(); alert('MY_CONST: ' + CONFIG.get('MY_CONST')); // 1 CONFIG.MY_CONST = '2'; alert('MY_CONST: ' + CONFIG.get('MY_CONST')); // 1 CONFIG.privates.MY_CONST = '2'; // error alert('MY_CONST: ' + CONFIG.get('MY_CONST')); // 1





I would like to have feedback from my blog readers. Your valuable feedback, question,
or comments about this article are always welcome.

Friday, 3 January 2014

using selector cache to avoid requery

 <script>
/* Title: selector cache Description: using selector cache to avoid requery */ // antipattern $('.list-item').click(function () { $('.photo').hide(); }); // preferred var $photo; // prefix the cache with $ to help identify it as a selector cache later $('.list-item').click(function () { $photo = $photo || $('.photo'); $photo.hide(); }); </script>
I hope you will enjoy the tips while playing with JavaScript.I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

jquery patterns $.data

<script> /* Title: data * Description: pattern and antipattern of using data */ // antipattern $(elem).data(key, value); // preferred $.data(elem, key, value); </script>
I hope you will enjoy the tips while playing with JavaScript.I would like to have feedbackfrom my blog readers. Your valuable feedback, question, or comments about this article 
are always welcome.

jquery patterns append

<script> /* Title: append * Description: use string concatenate and set innerHTML */ // antipattern // appending inside $.each(reallyLongArray, function (count, item) { var newLI = '<li>' + item + '</li>'; $('#ballers').append(newLI); }); // documentFragment off-DOM var frag = document.createDocumentFragment(); $.each(reallyLongArray, function (count, item) { var newLI = $('<li>' + item + '</li>'); frag.appendChild(newLI[0]); }); $('#ballers')[0].appendChild(frag); // string concatenate and set innerHTML var myhtml = ''; $.each(reallyLongArray, function (count, item) { myhtml += '<li>' + item + '</li>'; }); $('#ballers').html(myhtml); </script>
I hope you will enjoy the tips while playing with JavaScript. I would like to have feedbackfrom my blog readers. Your valuable feedback, question, or comments about this article 
are always welcome.