0

Why does in the following code, i don't see output as T12345 ABC and instead i see undefined undefined.

The reason i am expecting T12345 ABC because myCarDetails now refer to global context and global context have registrationNumber and brand defined.

var registrationNumber = "T12345";
var brand =  "ABC";

var car = { 
    registrationNumber: "T12345",
    brand: "ABC",

    displayDetails: function(){
        console.log(this.registrationNumber + " " + this.brand);
    }
}

var myCarDetails =  car.displayDetails;
myCarDetails();
simbada
  • 940
  • 4
  • 24
  • 43
  • Possible duplicate of [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Steven Stark Mar 11 '19 at 20:29
  • May i know why this is duplicate, The question you referred is an overview of this operator whereas i am talking about specific problem under 'this' umbrella ! – simbada Mar 11 '19 at 20:31
  • 4
    What execution environment are you using? Your code works exactly as you expect it to work in Firefox via the console. – Pointy Mar 11 '19 at 20:32
  • @Pointy i am using developer.mozilla.org – simbada Mar 11 '19 at 20:33
  • huh? No I mean how are you running the code? – Pointy Mar 11 '19 at 20:33
  • 4
    While `this` refers to the global object, `var registrationNumber` and `var brand` may not be defined in global scope. It really depends on how the whole piece of code is executed. – Felix Kling Mar 11 '19 at 20:33
  • On Mozilla, there is live javascript code run option. right? i am using that – simbada Mar 11 '19 at 20:34
  • @simbada you are asking about scope of 'this', which is a very common and often duplicated question on SO that has been answered countless times. – Steven Stark Mar 11 '19 at 20:34
  • 2
    There are likely not evaluating the code in global scope. If you want to ensure that `registrationNumber` and `brand` are defined in global scope, do `window.registrationNumber = ...;` instead. – Felix Kling Mar 11 '19 at 20:34
  • 1
    Ah well it must be the case that what appears to be the global (`window`) scope is actually *not* the global scope in that environment. – Pointy Mar 11 '19 at 20:34
  • Hi, Just it should work fine, I tested in chrome – Mahamudul Hasan Mar 11 '19 at 20:35
  • @pointy great i see your point and now getting close to find the reason, By the way if it has been global scope window then the output should be what i am expecting. Correct? – simbada Mar 11 '19 at 20:36
  • 1
    Yes, exactly — if you copy the exact code you posted and paste it into your browser developer console, it should work (it did for me in Firefox) – Pointy Mar 11 '19 at 20:41
  • 2
    You can do a simple test: `var simbada1 = "Hello World!"; console.log(window.simbada1);` If that says `undefined` then the outer scope of the execution environment is not really the global scope. – Pointy Mar 11 '19 at 20:42
  • @Pointy great. all clear. – simbada Mar 11 '19 at 20:44

1 Answers1

0

When we try to access any property of an Object which is a function itself, in this case way to access this property is -

var myCarDetails = car.displayDetails();

myCarDetails

Complete Working code is as Follows -->

var car = { registrationNumber: "T12345",

         brand: "ABC",

         displayDetails:  function() {

         console.log(this.registrationNumber + " " + this.brand);
         }
       }

var myCarDetails = car.displayDetails();

myCarDetails;

Additional Resource --> https://www.w3schools.com/jS/js_objects.asp

user9916003
  • 15
  • 1
  • 1