Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

node.js - What does ({"key": "value"} = {}) syntax mean inside a JavaScript function

I am doing a JavaScript course (specifically the MongoDB University M220JS course) and in one of the tasks I've come across this syntax for a function declaration inside a class:

static async getMovies({
   filters = null,
   page = 0,
   moviesPerPage = 20,
} = {}) {
   // some code, e.g.
   console.log(moviesPerPage)
}

Where they've defined some JSON object and then written '= {}' inside the parameter list. The variables are then used inside the function (as I've shown) as if they've been declared like default parameters like so:

static async getMovies(filters = null, page = 0, moviesPerPage = 20) {
   // some code, e.g.
   console.log(moviesPerPage)
}

What does the syntax in the first example mean? What is the significance of the = {} part?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What does the syntax in the first example mean?

the used syntax is a combination of default parameters syntax and destructuring assignment syntax

What is the significance of the '= {}' part?

using the default parameters syntax: the '= {}' part set {} (an empty object) as the default value for the single parameter taken by getMovies

let's break it down:

here, we set 'flying' as default value for the power parameter

function giveSuperPower(power='flying'){
    console.log(`you have got: "${power}"`);
}

giveSuperPower('infinite energy');
// you have got: "infinite energy"
giveSuperPower();
// you have got: "flying"

what if the parameter was an object?

function giveSuperPower(power){
    console.log(`you have got: "${power.name||'flying'}"`);
    console.log(`it last for ${power.duration||'∞'}min`);
}
giveSuperPower({name: 'strength', duration: 5 });
// you have got: "strength"
// it last for 5min

looks good.. right? lets try

giveSuperPower();
// Uncaught ReferenceError: power is not defined

okay that's a problem.

lets set a default parameter value (an empty object)

function giveSuperPower(power={}){
    console.log(`you have got: "${power.name||'flying'}"`);
    console.log(`it last for ${power.duration||'∞'}min`);
}

giveSuperPower();
//you have got: "flying" 
//it last for ∞min

let's destructure the power power parameter

function giveSuperPower({name, length} = {}){
    console.log(`you have got: "${name||'flying'}"`);
    console.log(`it last for ${duration||'∞'}min`);
}

let's set default parameter values for name and length

function giveSuperPower({name = 'flying', length = '∞'} = {}){
    console.log(`you have got: "${name}"`);
    console.log(`it last for ${duration}min`);
}

that's it :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...