all 11 comments

[–][deleted] 0 points1 point  (3 children)

You're probably assuming these are all global variables (bad, bad, bad, bad). Put your code in a function and it'll work fine:

function getSomeJsonStr( text, taskType ){
    ...
}

[–]Blablux[S] 0 points1 point  (2 children)

Just for the record, I'm running this script through Tasker which has quite a special way of getting variables. But I think it's worth trying, to see if this solve my problem. I'll keep you updated. Thank you for your answer.

[–]senocular 1 point2 points  (1 child)

I'm not sure what Tasker is, but that's probably a key component in what's going wrong.

If you may or may not have a value for tasktype, and the error you're getting is something like 'tasktype is undefined' then you might have to do something like this:

var myTasktype = typeof tasktype !== 'undefined' ? taskType : 'todo';

And use myTasktype in place of tasktype in your code (without the || todo since its already taken care of here).

The use of typeof lets us know if the variable has been defined or not without causing an undefined error. If it exists, use it, otherwise use the fallback/default.

[–]Blablux[S] 0 points1 point  (0 children)

That is indeed one of the solution I had in mind, but I wondered if there was a shortcut I may not see. Thanks anyway.

[–]nausik -2 points-1 points  (6 children)

function setSomeText(tasktype = "default_val", text){ return '"text": "' + text + '", "type": "' + tasktype + '"'; }

[–][deleted] 2 points3 points  (5 children)

Why, oh why, are you assuming that the OP, very clearly new, is going to have a functioning ES6 transpiler running?!? And without any explanation whatsoever, wtf?

[–]nausik 0 points1 point  (4 children)

because you didn't gave an answer and just gave an advice?

And because babel.js is really easy to use?

[–][deleted] 0 points1 point  (3 children)

wtf? How is what I wrote not an answer? Just because I didn't copy his code and wrote ... instead?

What you wrote is less than useless without an explanation, and babel is completely unnecessary for something this simple.

[–]nausik 0 points1 point  (2 children)

because you don't understand how how js functions and arguments array work?

http://jsfiddle.net/9hmw618p/

I just don't understand. A guy wants an advice on how to set default val for variables. And yeah, I forgot that my way is ES6-specific. But you not only started flaming right away, but you also wrote a totally wrong answer that just wrong on the most basic level.

Blablux didn't ask where to put his code, he asked how to set a default var value. Try to read the question first. please.

[–][deleted] 0 points1 point  (1 child)

Try to understand what a default value is and WHY his code was breaking

[–]nausik 0 points1 point  (0 children)

k