19th Mar 2020, 2:41 AM
Alireza Babaei
Alireza Babaei - avatar
3 Respostas
+ 3
The undefined seems to be just a place holder for the global object. According to the note below you could have used (null, undefined or a primitive value). "Note that this may not be the actual value seen by the method: if the method is a function in non-strict mode code, null and undefined will be replaced with the global object, and primitive values will be boxed. This argument is required." MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply https://code.sololearn.com/WzQ908T6K8kp/#js
19th Mar 2020, 3:53 AM
ODLNT
ODLNT - avatar
+ 2
Your code : ``` let sum = function(a, b) { console.log(a + b); } sum.apply(undefined, [1, 2]); // 3 ``` There should be a semicolon after body of anonymous function (which you are assigning to variable sum). Javascript applies automatic semicolon insertion after a line break but not in all cases. In case of assignment it will not insert a semicolon if there is no line break. Example, let a = 5, b; Here you initialize `a` and define b. Javascript expects "a semicolon" or "a line break" or "a valid identifier" after let a = 5, The same applies to your code. Correct : +Use semicolon ; ``` let sum = function(a, b) { console.log(a + b); }; sum.apply(undefined, [1, 2]); // 3 ``` +Or use line break ``` let sum = function(a, b){ console.log(a + b); } sum.apply(undefined, [1, 2]); // 3 ```
19th Mar 2020, 2:52 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar