1

In the code below, I was trying to create a constructor object, then I wondered does it work as a function while It is an object ...It actually works but the result is not desirable...like it ignores "jack" and "joe" as an property in the object but when I use it as a pure object it works properly... I wanted to know is this action logical or not??

function Lome() {
      let object1 = { z: 15, h: 67 };
      this.jack = 16;
      this.joe = { x: 5, y: 8 };
      Object.defineProperty(this, "john", {
        get() {
          return object1;
        },
        set(value) {
          object1 = value;
        },
        enumerable: true,
      });
      return object1;
    }
    const lara = new Lome();
    lara.john = 6;
    console.log(Lome());
    //{ z: 15, h: 67 };
    console.log(JSON.stringify(lara));
    //{"z":15,"h":67,"john":6};
Lome G ner
  • 21
  • 4
  • 1
    Did you try to console log `this` inside the function definition? – DreamBold Nov 28 '22 at 19:14
  • It doesn't give a good output,,, it prints both global object and the properties of my object and even if It was correct ... I wanted to apply some changes on the properties of my object ,,,as you can see I've written lara.john=6 in line 17(that's the change) and by your logic I was unable to do that @Dream Bold – Lome G ner Nov 28 '22 at 19:34
  • A constructor that does `return object1;` is not really a constructor. You should `"use strict";` mode, and you should rely on `this` being implicitly returned. – Bergi Nov 28 '22 at 21:13
  • so I realized the function is considering `jack`,`joe` and `john` as properties of `global object`, the question is why??? because I have defined `const lara=new Lome()` to consider those as properties of `lara` but still they are members of `global object`,,,and why the output for `console.log(JSON.stringify(lara))` is `{"z":15,"h":67,"john":6}`,,,how `john` can be part of the `global object` and `lara` at the same time and why `object 1` is a property of the `lara` ?@Bergi – Lome G ner Nov 29 '22 at 07:45
  • "*the function is considering jack,joe and john as properties of global object, the question is why?*" - only when you call `Lome()`, not the constructor `new Lome()`. See [how does the `this` keyword work](https://stackoverflow.com/q/3127429/1048572). "*how john can be part of the global object and lara at the same time*" - because you did `Object.defineProperty(this, "john", …)` on the global object, and `lara.john = 6;` on lara - they're two different properties. "*why object 1 is a property of the lara?*" - it is not. – Bergi Nov 29 '22 at 13:03
  • I realized the issue,when we return an object in a constructor function, the object which is holding the property and methods of that specific constructor(in this example lara), would ignore all of its own property and methods(in this example jack and joe and john) and would return that object(in this example object1),,,for more information read new document on MDN. – Lome G ner Dec 20 '22 at 12:28

1 Answers1

1

You misunderstood the this keyword in the context of javascript. By default, it generally refers to the window object, and when it's used in use strict mode, it's undefined.

In a function, the global object is the default binding for this. In a browser window the global object is [object Window]:

You can read more about it here or on W3schools

DreamBold
  • 2,727
  • 1
  • 9
  • 24