Consider the following JavaScript code snippet var car com

Consider the following JavaScript code snippet var car = { company: \"Porche\", model : \"911\" description : function() f return this.company + \"\"+this ny+ \"\"this var z = car.description; After this executes, what value does z have? Select one: a. 911 b. \"Porche 911\" c. function 0 return this.company+\"\"+ this.model; )

Solution

As JavaScript being a functional language, it allows functions can be used either as Key or Value.

1.   In the given piece of code, car is a dictionary.

   1.1   Keys of dictionary car are company, model, description.
   1.2   For keys company and model, strings are assigned as values.
   1.3 For key description, function is used which in-turn treated as Object.
  
2.   Key description of dictionary car holds the function definition.

3. Statement var z = car.description; stores complete function definition to variable z

   3.1 So variable z will have string: function() { return this.company + \" \" + this.model; }

4.   In order to make the variable z to hold return value of function, rewrite the statement as:

   Old:   var z = car.description; // Z holds: function() { return this.company + \" \" + this.model; }
   New:   var z = car.description();   // z holds: Porche 911
  
   Observe a opening and closing braces, that executes the function and returns value \"Porche 911\" and stores in variable z.

  
5. As key description is called with out braces, it holds the value \"function() { return this.company + \" \" + this.model; }\"

Hence, correct option is (c)   

 Consider the following JavaScript code snippet var car = { company: \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site