0

I created a function like this:

window.SetUploader = function(action, elementID, multiple, allowedExtensions) {
    //Optional arguments
    if (actionUploader == null) {
      actionUploader = "/Upload";
    }
    if (elementIDUploader == null) {
      elementIDUploader = "file-uploader";
    }
    if (multipleUploader == null) {
      multipleUploader = false;
    }
    if (allowedExtensions == null) {
      allowedExtensions = [];
    }

    //Function
     .....
};

To call this function, use the following code:

SetUploader('/Projects/ImageUpload', 'Logo', { allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'zip'] });

Note the parameter allowedExtensions, it does not change the value.

Debugging function, allowedExtensions value is null.

What is wrong?

1
  • You are referring to a different variable name in your if test. Commented Nov 11, 2011 at 14:06

6 Answers 6

1

You are passing your actual allowedExtendsions parameter as the third argument which is where the multiple formal parameter appears. Put undefined before it.

SetUploader(
   /* action */    '/Projects/ImageUpload', 
   /* elementId */ 'Logo', 
   // NEED VALUE FOR MULTIPLE HERE
   ['jpg', 'jpeg', 'png', 'gif', 'zip'])

You are also never using the action parameter, instead checking and setting a global variable actionUploader.

Typically when allowing optional arguments you have one particular argument which is a map of extra arguments.

function (requiredParam, anotherRequiredParam, extra) {
  var optionalParam = extra ? extra.optionalParamName : defaultValue;
  ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, but I want to ignore this value, let him take the default value defined in the function!
@RidermandeSousaBarbosa, so you want to ignore the value of a particular parameter and use one defined in the function regardless of what the caller passes in?
1

You are passing your object as third parameter (as multiple), your allowedExtensions is fourth.

Also you are checking variables with "Uploader" as name suffix. They are not variables that you pass.

Next, if you would pass { allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'zip'] } as fourth parameter to get hold of array with extensions you would have to write allowedExtensions.allowedExtensions

Comments

1

First of all your variables are not null, they are undefined and you need to check with the === operator. undefined and null are not the same, variables in JavaScript that have the declared but not initialized have the value undefined. With the === operator you make JavaScript check the type and the values you are comparing. If you only use the == operator JavaScript will attempt to type convert your variables making this:

if (allowedExtensionsUploader == null)

into this:

if (false == false)

which can lead to problems.

Secondly you only provide three arguments to your function but you're asking about the fourth.

Thirdly, the fourth argument is called allowedExtensions but you are checking a variable called allowedExtensionsUploader.

Fourthly, I think you should use jslint to check your code. It would have told you a lot of what I just wrote =)

2 Comments

Actually I use [coffeescript.org/] and this is the javascript code that generates this tool when I use something like: window.SetUploader = (action = "/Upload", elementID = "file-uploader", multiple = false, allowedExtensions = []) -> //code
See this tool running online: js2coffee.org Copy and paste the code in tab Coffee -> JS: CODE: window.SetUploader = (action = "/Upload", elementID = "file-uploader", multiple = false, allowedExtensions = []) -> ### code ###
0

Since you are passing it as the parameter multiple, as it is the third parameter.

In JavaScript you cannot have named parameters.

 { allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'zip'] }

Is an object that has a property allowedExtensions init to an array.

Comments

0

you are passing only three arguments in the function call

the parameter

{ allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'zip'] }

is a JSON object that will be available in "multiple" variable

Try adding one more argument in the function call, you will get that value as the 4th parameter "allowedExtensions"

Comments

0
  1. Your arguments do not match your variables in your if statements.
  2. You should check whether the variables are undefined, not null.
  3. You should use ===, not ==.

Use this instead:

window.SetUploader = function(actionUploader, elementIDUploader, multipleUploader, allowedExtensionsUploader) {
    //Optional arguments
    if (actionUploader === undefined) {
      actionUploader = "/Upload";
    }
    if (elementIDUploader === undefined) {
      elementIDUploader = "file-uploader";
    }
    if (multipleUploader === undefined) {
      multipleUploader = false;
    }
    if (allowedExtensionsUploader === undefined) {
      allowedExtensionsUploader = [];
    }
};

SetUploader('/Projects/ImageUpload', 'Logo', undefined, ['jpg', 'jpeg', 'png', 'gif', 'zip']);

2 Comments

Sorry, the parameter name was correct. By moving to the stackoverflow, I wrote wrong. About null and ==, I use coffee-script, and this was the code it generated. Even fixing the variable name allowedExtensions (see edit), I do not know how to call this function passing only the first two and the last parameter
@RidermandeSousaBarbosa - Just pass in undefined (or null, if you're checking against that). I just updated my answer, see the last line.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.