JavaScript How to handle undefined function arguments

The below code is an attempt to create a modular code block, which silently fails in case jQuery (dependency) is not present.

(function($, undefined){
    if ( $ === undefined ){
        // Do some stuff here
        return;
    }

    // Rest of the code
})(jQuery);

But it throws the following JS error when I don't include jQuery, which is the scenario I really wanted to test in the first place.

Uncaught ReferenceError: jQuery is not defined

I had to resort to a much uglier version of the code.

(function($, undefined){
    if ( $ === null ){
        // Do some stuff here
        return;
    }

    // Rest of the code
})( (typeof jQuery !== "undefined") ? jQuery : null );

Does anybody know a better way of doing this?

Answer:1

Can be done like this if for any reason you need it:

(function($, undefined){
    if ( $ === undefined ){
        alert('not defined');
        return;
    }

    // Rest of the code
})(window.jQuery); //if property on global object not defined, no error is thrown
Answer:2

I have the following following code: for (i = 0; i < 5; i++) { this.hands[0].cards[i].img.on('mousedown', (function (i) { var j = i; return function (event) { self....

I have the following following code: for (i = 0; i < 5; i++) { this.hands[0].cards[i].img.on('mousedown', (function (i) { var j = i; return function (event) { self....

I am writing in Casper Javacript and I am writing a test for a search bar object that tests each letter of the alphabet, by itself. So it will test 'a' then 'b' and so on. I am using the sendKeys ...

I am writing in Casper Javacript and I am writing a test for a search bar object that tests each letter of the alphabet, by itself. So it will test 'a' then 'b' and so on. I am using the sendKeys ...

  1. reset search field jquery
  2. datatables reset search field

My HTML: <div id="listViewBoxOffice" data-win-control="WinJS.UI.ListView" data-win-options="{ itemTemplate: select('#movieThumbnailTpl'), selectionMode: 'single' }"> </div> ...

My HTML: <div id="listViewBoxOffice" data-win-control="WinJS.UI.ListView" data-win-options="{ itemTemplate: select('#movieThumbnailTpl'), selectionMode: 'single' }"> </div> ...

  1. listview doesn't scroll
  2. listview doesn't scroll flutter
  3. listview doesn't show items c#
  4. listview doesn't show
  5. listview doesn't show items
  6. listview doesn't scroll android
  7. listview doesn't update
  8. listview doesn't scroll inside scrollview
  9. listview doesn't update c#
  10. listview doesn't show columns
  11. listview doesn't update android
  12. listview doesn't scroll xamarin
  13. listview does not change
  14. list view doesn't
  15. listview setitemchecked doesn't work
  16. flutter listview doesn't scroll
  17. javafx listview doesn't show items
  18. wpf listview doesn't update
  19. wpf listview doesn't scroll
  20. c# listview doesn't show columns

I am trying to return an array of values for the first <td> of every <tr> within <tbody> of my table. A demo here (attempt, not working) http://jsfiddle.net/vJqeT/1/ My JS Code: ...

I am trying to return an array of values for the first <td> of every <tr> within <tbody> of my table. A demo here (attempt, not working) http://jsfiddle.net/vJqeT/1/ My JS Code: ...

  1. return array values javascript
  2. return array values java
  3. return array values excel
  4. return array values php
  5. return array values c#
  6. return array values
  7. return array values js
  8. return array values in python
  9. return array values function
  10. php return array values only
  11. return all array values php
  12. php return array values by key
  13. return all array values c#
  14. sql return array values
  15. jquery return array values
  16. return array of unique values javascript
  17. return multiple values array javascript
  18. excel formula return array values
  19. return array of matching values excel
  20. php function return array values