1

In this code what does undefined mean? Without specifying undefined it says " my name is undefined and i am a undefined"

(function(){
    'use strict';

    function theFunction(name, profession) {
        console.log("My name is " + name + " and I am a " + profession + " . ");
    }
    theFunction("John", "fireman");
    theFunction.apply(undefined,["ali", "Developer"]);
    theFunction.call(undefined, "sara", "doctor");
}());
George Kagan
  • 5,913
  • 8
  • 46
  • 50
lana
  • 281
  • 1
  • 3
  • 10
  • See: http://stackoverflow.com/questions/5247060/in-javascript-is-there-equivalent-to-apply-that-doesnt-change-the-value-of-thi it's so you don't change the value of `this`. – scrappedcola Nov 04 '16 at 19:18
  • 1
    _"Without specifying undefined it says " my name is undefined and i am a undefined""_ Where? – guest271314 Nov 04 '16 at 19:19
  • I think OP means, if he doesn't add `undefined` as the first parameter when using apply or call. The correct answer is below. – Wout De Rooms Nov 04 '16 at 19:22

2 Answers2

6

My answer assumes that by Without specifying undefined you mean a call like that:

 theFunction.apply(["ali", "Developer"]);

When you use call or apply, the first parameter is the execution context (variable this inside theFunction). The two examples set it to undefined, so this inside theFunction will evaluate to undefined. For example:

function theFunction(name, profession) {
      console.log(this); // logs `undefined`
      console.log("My name is " + name + " and I am a " + profession + " . ");
}

theFunction.apply(undefined, ["ali", "Developer"]);

Here is the thread explaining why one would use undefined as execution context.

Now, to your question. If you omit undefined in your call is like that:

theFunction.apply(["ali", "Developer"]);

the execution context - this - is set to ["ali", "Developer"], and name and profession are evaluated as undefined since you only pass one parameter to apply, that is why you're getting "My name is undefined and I am a undefined"

call and apply are usually used when you want to change the execution context of the function. You're probably using apply to turn array of arguments into separate arguments. To do that, you need to set the same execution context as the one that would have been if you called the function without apply:

theFunction("John", "fireman"); // `this` points to `window`
theFunction.apply(this, ["John", "fireman"]); // `this` points to `window`
Community
  • 1
  • 1
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • 2
    `this` is never used anywhere, and the code outputs not what the OP says it does. – GSerg Nov 04 '16 at 19:19
  • I was gonna say, some code would explain more, but you beat me to it :) – Wout De Rooms Nov 04 '16 at 19:19
  • OP does not use `theFunction.apply(["ali", "Developer"])` at `javascript` at Question. Though OP might have tried `theFunction()` – guest271314 Nov 04 '16 at 19:21
  • Okay, I now understand what "Without specifying undefined" meant. They posted the code with specifying undefined and questioned the result of not specifying undefined. This answer is correct then. – GSerg Nov 04 '16 at 19:22
  • OP's example doesn't utilize the true meaning of call and apply (override `this`), it's exactly the same as simply calling the function directly. – George Kagan Nov 04 '16 at 19:23
  • @guest271314, well, I added assumption about how the function is called at the top of my answer. I based my answer on that assumption – Max Koretskyi Nov 04 '16 at 19:27
  • @Maximus You could also extrapolate that OP tried `theFunction()` – guest271314 Nov 04 '16 at 19:35
  • @Maximus you assumed right, I could have written my question more clearly. Thank you for your help – lana Nov 04 '16 at 19:35
  • @codeforfun, cool, if you think there's anything else I can add to make my answer acceptable, let me know – Max Koretskyi Nov 04 '16 at 19:36
  • 1
    @timenomad, thanks for the suggestion, added that clarification at the bottom of the question – Max Koretskyi Nov 04 '16 at 19:37
  • @codeforfun Consider updating Question to include all of the calls you tried? – guest271314 Nov 04 '16 at 19:37
1

Though theFunction() is not included as one of the calls tried, theFunction() reproduces result described at Question

Without specifying undefined it says " my name is undefined and i am a undefined"

that is, to call theFunction() without passing parameters; which would be expected result where name and profession are undefined within function body when theFunction is called.

(function() {
  'use strict';

  function theFunction(name, profession) {
    console.log("My name is " + name + " and I am a " + profession + " . ");
  }
  theFunction(); // logs result described at Question
  theFunction("John", "fireman");
  theFunction.apply(undefined, ["ali", "Developer"]);
  theFunction.call(undefined, "sara", "doctor");

}());
guest271314
  • 1
  • 15
  • 104
  • 177
  • this call `theFunction.apply(["ali", "Developer"]);` would produce the result the OP gets `My name is undefined and I am a undefined . ` – Max Koretskyi Nov 04 '16 at 19:29
  • @Maximus Edited Answer; though both `theFunction()` and `theFunction.apply(["ali", "Developer"])` are not included as what OP actually tried. None of the listed calls at Question return result described at Question. – guest271314 Nov 04 '16 at 19:31
  • you're right. Your assumption is as possible as mine. Anyways, I upvoted your answer since it shows the other possible alternative. – Max Koretskyi Nov 04 '16 at 19:39