Look at following codes about parameter destructuring. Indicate what’s the outpu
ID: 3744618 • Letter: L
Question
Look at following codes about parameter destructuring. Indicate what’s the output and why
function getSentence({ subject, verb, object }) {
return `${subject} ${verb} ${object}`;
}
const o = {
subject: "I",
verb: "love",
object: "JavaScript",
};
getSentence(o);
------------------------------------
function getSentence({ subject, verb, object }) {
return `${subject} ${verb} ${object}`;
}
const o = {
subject: "I",
verb: "love",
object: "JavaScript",
};
getSentence(o);
----------------------------------------
function addPrefix(prefix, ...words) {
const prefixedWords = [];
for (leti=0; i<words.length; i++) {
prefixedWords[i] = prefix + words[i];
}
return prefixedWords;
}
addPrefix("con", "verse", "vex");
----------------------------------------
2) Look at following codes about passing values into functions. Indicate what’s the output and why
function f(x) {
console.log(`inside f: x=${x}`);
x = 5;
console.log(`inside f: x=${x} (after assignment)`);
}
let x = 3;
console.log(`before calling f: x=${x}`);
f(x);
console.log(`after calling f: x=${x}`);
----------------------------------------
function f(o) {
o.message = `set in f (previous value: '${o.message}')`;
}
let o = {
message: "initial value"
};
console.log(`before calling f: o.message="${o.message}"`);
f(o);
console.log(`after calling f: o.message="${o.message}"`);
----------------------------------------
function f(o) {
o.message = "set in f";
o = {
message: "new object!"
};
console.log(`inside f: o.message="${o.message}" (after assignment)`);
}
let o = {
message: 'initial value'
};
console.log(`before calling f: o.message="${o.message}"`);
f(o);
console.log(`after calling f: o.message="${o.message}"`);
Explanation / Answer
//Output
1. I love javascript
because we are passing o object to function getsentence() which object of type o and prints its attributes
2. same as ist getsentance() same used
3. converse and convex will be output because addprefix() uses one two arguents in which one is variable argument so con gets into first parameter and vex and verse gets into second.
4. before calling f: x=3
inside f: x=3
after calling f: x=5
inside f: x=5 (after assignment)
since value of x was 3 ist calling it will print x=3
then we are changing value to 5
and caling again the function
5. set in f (previous value: initial value)
before calling f: o.message=initial vaue
after calling f: o.message=initial vaue
because we are not changing the value of o;
6. before calling f: o.message=initial value
inside f: o.message="new object!" (after assignment)
after calling f: o.message="new object"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.