1

I'm working on a cool program on the khanacademy.org javascript/ProcessingJS canvas, and I want to use default parameters. However, it showed this error box:

'default parameters' is only available in ES6 (use esnext option).

What is that and how do I fix it?

morphatic
  • 7,677
  • 4
  • 47
  • 61
Pocketkid2
  • 803
  • 1
  • 11
  • 16
  • If I recall it is an issue with JS 2.6.x, not 100% sure beyond this. – Lilith Daemon Jul 14 '15 at 03:55
  • I feel like there is a solution to this, as prompted by the esnext option in the message itself. However, I have no idea what that means, how to use it, or if it's possible. – Pocketkid2 Jul 14 '15 at 03:57
  • Not sure what to suggest in regards to fixing the specific problem, especially without more information; but ES6 is JS 2.6.x if I recall. I am told it changed a great deal of the syntax of the language, so this would probably be the best place to start looking. I don't use JS nearly enough to say what specifically would be causing this error. – Lilith Daemon Jul 14 '15 at 04:01

1 Answers1

0

Using default parameters in this way will give the warning you mentioned:

var foo = function(param1, param2 = "some default value"){
  console.log(param1 + " " + param2);
}

Assigning default parameters in the way described above is an ECMAScript 6 feature and is currently only supported by Mozilla Firefox. Check browser compatibility here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/default_parameters#Browser_compatibility

Generally default parameters are assigned in javascript in the following way. I suggest you to follow the same, they will work fine in all browsers:

var foo = function(param1, param2){
   param2 = typeof param2 !== 'undefined' ?  param2 : "some default value";
   console.log(param1 + " " + param2);
}

Check this link for more details: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/default_parameters

thereisnospoon
  • 255
  • 2
  • 8