Asked  7 Months ago    Answers:  5   Viewed   34 times

If I want to call a function like this:

moo({ a: 4 });

Normally I'd have to phrase my function definition like this:

function moo(myArgObj) {

But this awesome syntax is totally valid in spidermonkey for defining functions:

function moo({ a, b, c }) { // valid syntax!
    print(a); // prints 4

What is this feature?



It's called destructuring. You might find the most info at MDN: Destructuring assignment (especially see Unpacking fields from objects passed as function parameter).

The ECMAScript standards discussion can be found on their wiki page, also interesting might be this blog post at dailyjs.

Tuesday, June 1, 2021
answered 7 Months ago

I love 10 ways to format time and date using JavaScript and Working with Dates.

Basically, you have three methods and you have to combine the strings for yourself:

getDate() // Returns the date
getMonth() // Returns the month
getFullYear() // Returns the year


var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
console.log(curr_date + "-" + curr_month + "-" + curr_year);
Tuesday, June 1, 2021
answered 7 Months ago

tmLanguage scope names are based on convention, so there's no definitive list. The "Naming Conventions" section at the bottom of this TextMate Manual page is fairly comprehensive though.

You can also check what scopes are highlighted by VSCode's default themes, see for instance dark_plus.json and dark_vs.json (which the former is based on / includes).

Finally, as of VSCode 1.9.0, there is a builtin command to inspect tmLanguage scopes (Developer: Inspect TM Scopes). It will give you a lot of information over how the token at the cursor location is scoped / highlighted:

There is also a Scope Info extension which does a very similar thing, but on hover (which I personally prefer usability-wise). However, it only lists the scope names, not including any of the additional info that VSCode's built-in inspector has.

Friday, June 25, 2021
answered 6 Months ago

You could destructure with a renaming and take the same property for destructuring.

const a = { b: { c: 'Hi!' } };
const { b: formerB, b: { c } } = a;

Thursday, July 29, 2021
answered 4 Months ago

Just replace = with ::

const {a, b, c: {e = 'default', f = 'default'}} = require('./something')


const { a, b, c: { e = 'default', f = 'default'} } = {a: 1, b: 2, c: {e: 3}}
console.log(`a: ${a}, b: ${b}, e: ${e}, f: ${f}`)

It prints:

a: 1, b: 2, e: 3, f: default
Thursday, August 12, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :