TW5 JS macros

146 views
Skip to first unread message

tejjyid

unread,
Mar 11, 2017, 12:50:06 AM3/11/17
to TiddlyWiki
It seems that when I execute a JS macro with an unsupplied parameter, it's set to "" rather than undefined. Is that always the case?

Thanks

PMario

unread,
Mar 11, 2017, 4:33:58 AM3/11/17
to TiddlyWiki
On Saturday, March 11, 2017 at 6:50:06 AM UTC+1, tejjyid wrote:
It seems that when I execute a JS macro with an unsupplied parameter, it's set to "" rather than undefined. Is that always the case?

Yes.

Macros return text and we need to be 100% compatible with wikitext macros. They work that way.

-m

Jeremy Ruston

unread,
Mar 11, 2017, 11:22:53 AM3/11/17
to tiddl...@googlegroups.com
JS macros can specify a default value for each parameter; otherwise you'll see "undefined" as the value. For example:
exports.params = [
	{name: "title", "default": "My title"}
];
Note that the property name "default" must be in quotes because it is a built-in keyword.

Best wishes

Jeremy

--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/39653d8f-9e02-4269-9f4d-2255e6adc6e9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

tejjyid

unread,
Mar 13, 2017, 5:15:52 AM3/13/17
to TiddlyWiki
OK, but I can accidentally return an undefined paramenter any time, surely? This slightly unexpected behaviour only protects against variables named in the function definition? Also, I notice Jeremy says "undefined".

Thanks

PMario

unread,
Mar 13, 2017, 5:33:38 AM3/13/17
to TiddlyWiki
On Monday, March 13, 2017 at 10:15:52 AM UTC+1, tejjyid wrote:
OK, but I can accidentally return an undefined paramenter any time, surely?

If you define your params like this. eg:

exports.params = [
    {"name":"tag", "default":""},
    {"name":"label", "default":"<$view field='title' format='text' />"},
    {"name":"tooltip", "default":""},
    {"name":"debug"}
    ];


Since params has to exist, there shouldn't be an accidents.
 
This slightly unexpected behaviour only protects against variables named in the function definition?

kind of. There is no mechanism in js, that tells you about returning undefined variables. The developer has to take care for this.
 
Also, I notice Jeremy says "undefined".

IMO it should show, that undefined is a string. In js undefined is the default state, if you define are variable, without initialisation.

undefined is not equal to the string "undefined"

-m

tejjyid

unread,
Mar 14, 2017, 12:52:44 AM3/14/17
to TiddlyWiki
I think I should listen to the guy here who always tells me "post code"!

1st, thanks for the new information. I hadn't previously seen the mechanism for adding default values through params; that's neat. 

The specific scenario I was envisaging looks a bit like this:

exports.name = "demo";
exports.params = [
    {"name":"p1"},
    {"name":"p2"}
    ];
exports.run = function(p1, p2) {
    console.log(typeof p2);
    console.log(p2);
    };

Then a tiddler containing:

<<demo "p1 data">>

results in a console log of String and nothing else. 

According to my reading on JS, what I should see is undefined undefined. 

If I understand you correctly, what you are saying is that TW overrides JS default behaviour in case someone tries to 

return p2;

incorrectly?
Reply all
Reply to author
Forward
0 new messages