0

I have created the following javascript code. This code is working fine but dreamweaver say, line (function load_unseen_notification(view = '')) something wrong. But what is the problem here code is working fine. I think the problem will come view ='' . How can i fix it?

function load_unseen_notification(view = '')
 {
  $.ajax({
   url:"fetch.php",
   method:"POST",
   data:{view:view},
   dataType:"json",
   success:function(data)
   {
    $('.dropdown-menu').html(data.notification);
    if(data.unseen_notification > 0)
    {
     $('.count').html(data.unseen_notification);
    }
   }
  });
 }
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
AlwaysStudent
  • 1,354
  • 18
  • 49
  • 3
    well I am guessing dreamweaver linter does not support ES6. – epascarello Nov 02 '17 at 14:55
  • That is exactly the problem. Javascript doesn't allow you to assign default values to your variables when you define functions. You will have to do it manually, check if it's defined and if it's not, assign an empty string. – Antoniu Livadariu Nov 02 '17 at 14:57
  • @AntoniuLivadariu https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters – Huangism Nov 02 '17 at 14:58
  • It is not a dupe on setting it, OP is asking why it is an error in the IDE... – epascarello Nov 02 '17 at 14:59
  • 4
    Change your settings to allow it https://stackoverflow.com/questions/45822265/adding-javascript-es6-to-dreamweaver-cs6 – epascarello Nov 02 '17 at 15:00
  • @Huangism haha apologies, was this added recently? I swear to god when I tried it a year ago it would throw errors so I had to do `if(variable === undefined)...` to assign default values to variables. But you are most right, I just tried it in the console and it works :O Should I delete the comment or? – Antoniu Livadariu Nov 02 '17 at 15:03
  • @AntoniuLivadariu that's up to you, ECMA6 added it I believe – Huangism Nov 02 '17 at 18:03

1 Answers1

2

DreaemWeaver is not using ES6 (which introduced default values for parameters) by default. But you can set this behavior in the settings. Just take a look at it.

JavaScript before ES6 doesn't support default values for parameters. But you can rewrite it to if you don't wnat to change the settings (which would be recommend):

function load_unseen_notification(view) {
  view = view || ''; // if view is defined, use the value. If not set view to an empty string.
Joshua K
  • 2,407
  • 1
  • 10
  • 13
  • Thanks for your answer. I understood what i am missing. But i get mines vote :( – AlwaysStudent Nov 02 '17 at 15:01
  • @Andreas yes i really understood it. Thank you. – AlwaysStudent Nov 02 '17 at 15:05
  • @Andreas I know. :) – AlwaysStudent Nov 02 '17 at 15:09
  • @Andreas you're right. Lost the focus. Is it now better? – Joshua K Nov 02 '17 at 15:09
  • Although the answer is correct and the way to assign default values prior to ES6 is correct, if you pass any falsey value to `view` it will define it as `''`, reason being that values such as `0` `''` `""` `null` `~-1`etc. all return `false` in conditionals, hence why they are called falsey. In this case if you wanted to assign `0` to `view` it wouldn't let you! A safer way would be to check `if(typeof view === 'undefined')` or just `if(view == undefined)`. – Antoniu Livadariu Nov 03 '17 at 09:23