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) {
    print(myArgObj.a);
}

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?

 Answers

44

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
 
Puneet
answered 7 Months ago
21

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

Example:

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
 
oroshnivskyy
answered 7 Months ago
92

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
 
laurent
answered 6 Months ago
100

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

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

console.log(formerB)
console.log(c);
Thursday, July 29, 2021
 
Manmay
answered 4 Months ago
59

Just replace = with ::

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

Demo:

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
 
tuckerjt07
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 :  
Share