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");
}());

3
  • See: stackoverflow.com/questions/5247060/… it's so you don't change the value of this. Commented Nov 4, 2016 at 19:18
  • 1
    "Without specifying undefined it says " my name is undefined and i am a undefined"" Where? Commented Nov 4, 2016 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. Commented Nov 4, 2016 at 19:22

2 Answers 2

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`
Sign up to request clarification or add additional context in comments.

11 Comments

this is never used anywhere, and the code outputs not what the OP says it does.
I was gonna say, some code would explain more, but you beat me to it :)
OP does not use theFunction.apply(["ali", "Developer"]) at javascript at Question. Though OP might have tried theFunction()
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.
@timenomad, thanks for the suggestion, added that clarification at the bottom of the question
|
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");

}());

3 Comments

this call theFunction.apply(["ali", "Developer"]); would produce the result the OP gets My name is undefined and I am a undefined .
@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.
you're right. Your assumption is as possible as mine. Anyways, I upvoted your answer since it shows the other possible alternative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.