1

Is it possible to create a nested method in javaScript like this:

Circle= function (){
  this.type1 = function (){
     this.property = {
       color: "red"
     }
  }
  this.type2 = function (){
     this.property = {
       color: "blue"
     }
  }
}

Where it could be accessed this way:

circle = new Circle();
circle.type2.property.color = "red";
Hyyan Abo Fakher
  • 3,497
  • 3
  • 21
  • 35
Starlyvil
  • 93
  • 1
  • 5
  • Possible duplicate of [Nested objects in javascript, best practices](https://stackoverflow.com/questions/7942398/nested-objects-in-javascript-best-practices) – Heretic Monkey Sep 10 '18 at 18:03
  • `this` is available inside functions and classes, not inside object literals. – connexo Sep 10 '18 at 18:08

1 Answers1

4

const Circle = function() {
  this.type1 = {
    property: {
      color: "red"
    }
  }
  this.type2 = {
    property: {
      color: "blue"
    }
  }
}

let c = new Circle();

c.type1.property.color = 'violet';

console.log(c.type1.property.color)
connexo
  • 53,704
  • 14
  • 91
  • 128