Skip to content

Commit e5cdc43

Browse files
authored
Update Notes.md
1 parent 8e1c0b5 commit e5cdc43

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

Ref/Notes.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,3 +1246,87 @@ Static class members (properties/methods) are not tied to a specific instance of
12461246
### Other Answers
12471247

12481248
* http://flowerszhong.github.io/2013/11/20/javascript-questions.html
1249+
1250+
## Topics Based
1251+
1252+
#spread-vs-rest-operators
1253+
1254+
Rest parameter: collects all remaining elements into an array.
1255+
When using rest arguments, you are collapsing all remaining arguments of a function into one array:
1256+
1257+
var [c, ...m] = [1,2,3,4,5]; // m -> [2,3,4,5]
1258+
Here ...m is a collector, it collects the rest of the parameters. Internally when we write:
1259+
1260+
var [c, ...m] = [1,2,3,4,5]; JavaScript does following
1261+
1262+
var c = 1,
1263+
m = [2, 3, 4, 5];
1264+
1265+
function add(x, y) {
1266+
return x + y;
1267+
}
1268+
1269+
add(1, 2, 3, 4, 5) // returns 3
1270+
1271+
function add(...args) {
1272+
let result = 0;
1273+
1274+
for (let arg of args) result += arg;
1275+
1276+
return result
1277+
}
1278+
1279+
add(1) // returns 1
1280+
add(1,2) // returns 3
1281+
add(1, 2, 3, 4, 5) // returns 15
1282+
1283+
function xyz(x, y, ...z) {
1284+
console.log(x, ' ', y); // hey hello
1285+
1286+
console.log(z); // ["wassup", "goodmorning", "hi", "howdy"]
1287+
console.log(z[0]); // wassup
1288+
console.log(z.length); // 4
1289+
}
1290+
1291+
xyz("hey", "hello", "wassup", "goodmorning", "hi", "howdy")
1292+
1293+
Before rest parameters existed, to get all the arguments in a function we used arguments which is an array-likeobject.
1294+
1295+
function someFunction() {
1296+
return arguments;
1297+
}
1298+
1299+
someFunction("joykare", 100, false);
1300+
1301+
someFunction returns the arguments and their indexes, [Arguments] { '0': 'joykare', '1': 100, '2': false }.
1302+
1303+
The downside of using the arguments keyword is that, it returns an array-like object; this means you essentially cannot perform any array-methods like; Array.filer, Array.map. Another pitfall, is that we cannot use arguments in arrow functions.
1304+
1305+
1306+
#Spread operator: allows iterables( arrays / objects / strings ) to be expanded into single arguments/elements.
1307+
1308+
The spread operator allows us to expand elements.
1309+
1310+
you are expanding a single variable into more:
1311+
1312+
var params = [ "hello", true, 7 ];
1313+
var other = [ 1, 2, ...params ]; // other => [1,2,"hello", true, 7]
1314+
Here, ...params spreads so as to assing all of its elements to other
1315+
1316+
Internally javaScript does following
1317+
1318+
var other = [1, 2].concat(params);
1319+
1320+
const arr = ["Joy", "Wangari", "Warugu"];
1321+
const newArr = ["joykare", ...arr];
1322+
1323+
const myNames = [...arr, "joykare"];
1324+
1325+
We can use the spread operator to copy an array.
1326+
1327+
const arr = [1, 2, 3];
1328+
const arr2 = [...arr];
1329+
1330+
## Explain how prototypal inheritance works
1331+
This is an extremely common JavaScript interview question. All JavaScript objects have a prototype property, that is a reference to another object. When a property is accessed on an object and if the property is not found on that object, the JavaScript engine looks at the object's prototype, and the prototype's prototype and so on, until it finds the property defined on one of the prototypes or until it reaches the end of the prototype chain. This behavior simulates classical inheritance, but it is really more of delegation than inheritance.
1332+

0 commit comments

Comments
 (0)