Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

A few things not pointed out in the above answers:

  1. You should place your code in a closure closure by creating an IIFE. This will create a private scope for your code so that you don't pollute the global scope.

    (function( $ ) { //code here })( jQuery );

In this example, I pass $ in so that jQuery can be safely referenced using it throughout your code.

  1. Use 'use strict';. Strict mode helps prevent a lot of common issues.

    (function( $ ) { 'use strict'; //code here })( jQuery );

  2. Cache your jQuery selections. Selecting elements is one of the slowest things that jQuery does. So creating variables at the top of your code to hold all of your selections. That way you can refer to this variable everywhere in your code.

    (function( $ ) { 'use strict'; var $master = $('.master'); //code here })( jQuery );

  3. You should create named functions instead of using anonymous functions named functions instead of using anonymous functions. This will help with debugging via stack traces. Anonymous functions are much harder to debug.

    (function( $ ) { 'use strict'; var $master = $('.master'); function masterClickHandler() { //code here } $master.on('click', masterClickHandler); })( jQuery );

  4. You don't need all of your code in document.ready. If you follow the above advice, you can just call your named functions that have already been created. This is another slight performance boost.

    (function( $ ) { 'use strict'; var $master; function masterClickHandler() { //code here } $(function(){ //shortcut for document.ready $master = $('.master'); $master.on('click', masterClickHandler); }); })( jQuery );

Hope that helps.

A few things not pointed out in the above answers:

  1. You should place your code in a closure by creating an IIFE. This will create a private scope for your code so that you don't pollute the global scope.

    (function( $ ) { //code here })( jQuery );

In this example, I pass $ in so that jQuery can be safely referenced using it throughout your code.

  1. Use 'use strict';. Strict mode helps prevent a lot of common issues.

    (function( $ ) { 'use strict'; //code here })( jQuery );

  2. Cache your jQuery selections. Selecting elements is one of the slowest things that jQuery does. So creating variables at the top of your code to hold all of your selections. That way you can refer to this variable everywhere in your code.

    (function( $ ) { 'use strict'; var $master = $('.master'); //code here })( jQuery );

  3. You should create named functions instead of using anonymous functions. This will help with debugging via stack traces. Anonymous functions are much harder to debug.

    (function( $ ) { 'use strict'; var $master = $('.master'); function masterClickHandler() { //code here } $master.on('click', masterClickHandler); })( jQuery );

  4. You don't need all of your code in document.ready. If you follow the above advice, you can just call your named functions that have already been created. This is another slight performance boost.

    (function( $ ) { 'use strict'; var $master; function masterClickHandler() { //code here } $(function(){ //shortcut for document.ready $master = $('.master'); $master.on('click', masterClickHandler); }); })( jQuery );

Hope that helps.

A few things not pointed out in the above answers:

  1. You should place your code in a closure by creating an IIFE. This will create a private scope for your code so that you don't pollute the global scope.

    (function( $ ) { //code here })( jQuery );

In this example, I pass $ in so that jQuery can be safely referenced using it throughout your code.

  1. Use 'use strict';. Strict mode helps prevent a lot of common issues.

    (function( $ ) { 'use strict'; //code here })( jQuery );

  2. Cache your jQuery selections. Selecting elements is one of the slowest things that jQuery does. So creating variables at the top of your code to hold all of your selections. That way you can refer to this variable everywhere in your code.

    (function( $ ) { 'use strict'; var $master = $('.master'); //code here })( jQuery );

  3. You should create named functions instead of using anonymous functions. This will help with debugging via stack traces. Anonymous functions are much harder to debug.

    (function( $ ) { 'use strict'; var $master = $('.master'); function masterClickHandler() { //code here } $master.on('click', masterClickHandler); })( jQuery );

  4. You don't need all of your code in document.ready. If you follow the above advice, you can just call your named functions that have already been created. This is another slight performance boost.

    (function( $ ) { 'use strict'; var $master; function masterClickHandler() { //code here } $(function(){ //shortcut for document.ready $master = $('.master'); $master.on('click', masterClickHandler); }); })( jQuery );

Hope that helps.

Corrected code issue
Source Link
Gary Storey
  • 1.1k
  • 6
  • 13

A few things not pointed out in the above answers:

  1. You should place your code in a closure by creating an IIFE. This will create a private scope for your code so that you don't pollute the global scope.

    (function( $ ) { //code here })( jQuery );

In this example, I pass $ in so that jQuery can be safely referenced using it throughout your code.

  1. Use 'use strict';. Strict mode helps prevent a lot of common issues.

    (function( $ ) { 'use strict'; //code here })( jQuery );

  2. Cache your jQuery selections. Selecting elements is one of the slowest things that jQuery does. So creating variables at the top of your code to hold all of your selections. That way you can refer to this variable everywhere in your code.

    (function( $ ) { 'use strict'; var $master = $('.master'); //code here })( jQuery );

  3. You should create named functions instead of using anonymous functions. This will help with debugging via stack traces. Anonymous functions are much harder to debug.

    (function( $ ) { 'use strict'; var $master = $('.master'); function masterClickHandler() { //code here } $master.on('click', masterClickHandler); })( jQuery );

  4. You don't need all of your code in document.ready. If you follow the above advice, you can just call your named functions that have already been created. This is another slight performance boost.

    (function( $ ) { 'use strict'; var $master = $('.master');$master; function masterClickHandler() { //code here } $(function(){ //shortcut for document.ready $master = $('.master'); $master.on('click', masterClickHandler); }); })( jQuery );

Hope that helps.

A few things not pointed out in the above answers:

  1. You should place your code in a closure by creating an IIFE. This will create a private scope for your code so that you don't pollute the global scope.

    (function( $ ) { //code here })( jQuery );

In this example, I pass $ in so that jQuery can be safely referenced using it throughout your code.

  1. Use 'use strict';. Strict mode helps prevent a lot of common issues.

    (function( $ ) { 'use strict'; //code here })( jQuery );

  2. Cache your jQuery selections. Selecting elements is one of the slowest things that jQuery does. So creating variables at the top of your code to hold all of your selections. That way you can refer to this variable everywhere in your code.

    (function( $ ) { 'use strict'; var $master = $('.master'); //code here })( jQuery );

  3. You should create named functions instead of using anonymous functions. This will help with debugging via stack traces. Anonymous functions are much harder to debug.

    (function( $ ) { 'use strict'; var $master = $('.master'); function masterClickHandler() { //code here } $master.on('click', masterClickHandler); })( jQuery );

  4. You don't need all of your code in document.ready. If you follow the above advice, you can just call your named functions that have already been created. This is another slight performance boost.

    (function( $ ) { 'use strict'; var $master = $('.master'); function masterClickHandler() { //code here } $(function(){ //shortcut for document.ready $master.on('click', masterClickHandler); }); })( jQuery );

Hope that helps.

A few things not pointed out in the above answers:

  1. You should place your code in a closure by creating an IIFE. This will create a private scope for your code so that you don't pollute the global scope.

    (function( $ ) { //code here })( jQuery );

In this example, I pass $ in so that jQuery can be safely referenced using it throughout your code.

  1. Use 'use strict';. Strict mode helps prevent a lot of common issues.

    (function( $ ) { 'use strict'; //code here })( jQuery );

  2. Cache your jQuery selections. Selecting elements is one of the slowest things that jQuery does. So creating variables at the top of your code to hold all of your selections. That way you can refer to this variable everywhere in your code.

    (function( $ ) { 'use strict'; var $master = $('.master'); //code here })( jQuery );

  3. You should create named functions instead of using anonymous functions. This will help with debugging via stack traces. Anonymous functions are much harder to debug.

    (function( $ ) { 'use strict'; var $master = $('.master'); function masterClickHandler() { //code here } $master.on('click', masterClickHandler); })( jQuery );

  4. You don't need all of your code in document.ready. If you follow the above advice, you can just call your named functions that have already been created. This is another slight performance boost.

    (function( $ ) { 'use strict'; var $master; function masterClickHandler() { //code here } $(function(){ //shortcut for document.ready $master = $('.master'); $master.on('click', masterClickHandler); }); })( jQuery );

Hope that helps.

Source Link
Gary Storey
  • 1.1k
  • 6
  • 13

A few things not pointed out in the above answers:

  1. You should place your code in a closure by creating an IIFE. This will create a private scope for your code so that you don't pollute the global scope.

    (function( $ ) { //code here })( jQuery );

In this example, I pass $ in so that jQuery can be safely referenced using it throughout your code.

  1. Use 'use strict';. Strict mode helps prevent a lot of common issues.

    (function( $ ) { 'use strict'; //code here })( jQuery );

  2. Cache your jQuery selections. Selecting elements is one of the slowest things that jQuery does. So creating variables at the top of your code to hold all of your selections. That way you can refer to this variable everywhere in your code.

    (function( $ ) { 'use strict'; var $master = $('.master'); //code here })( jQuery );

  3. You should create named functions instead of using anonymous functions. This will help with debugging via stack traces. Anonymous functions are much harder to debug.

    (function( $ ) { 'use strict'; var $master = $('.master'); function masterClickHandler() { //code here } $master.on('click', masterClickHandler); })( jQuery );

  4. You don't need all of your code in document.ready. If you follow the above advice, you can just call your named functions that have already been created. This is another slight performance boost.

    (function( $ ) { 'use strict'; var $master = $('.master'); function masterClickHandler() { //code here } $(function(){ //shortcut for document.ready $master.on('click', masterClickHandler); }); })( jQuery );

Hope that helps.

default

AltStyle によって変換されたページ (->オリジナル) /