Skip to main content
deleted 6 characters in body
Source Link
Eddie D
  • 1.1k
  • 7
  • 16

If you want to keep the actual array hidden and untouchable except through the methods, it has to be declared in the constructor. Any function that alters it has to be declared in the constructor as well. If that item is to be publicly accessible, it has to be attached to this.

class Post extends Array
{
  add(val)
  {
    this.unshift(val);
  }
  remove(val)
  {
    this.shift(val);
  }
}

class MyClass 
{
  constructor() 
  {
    this.date_created = new Date()
    
    this.post = new Post();
  }
}
let x = new MyClass();
console.log(x.post);
x.post.add(2);
console.log(x.post);

If you want to keep the actual array hidden and untouchable except through the methods, it has to be declared in the constructor. Any function that alters it has to be declared in the constructor as well. If that item is to be publicly accessible, it has to be attached to this.

class Post extends Array
{
  add(val)
  {
    this.unshift(val);
  }
  remove(val)
  {
    this.shift(val);
  }
}

class MyClass 
{
  constructor() 
  {
    this.date_created = new Date()
    
    this.post = new Post();
  }
}
let x = new MyClass();
console.log(x.post);
x.post.add(2);
console.log(x.post);

If you want to keep the actual array hidden and untouchable except through the methods, it has to be declared in the constructor. Any function that alters it has to be declared in the constructor as well. If that item is to be publicly accessible, it has to be attached to this.

class Post extends Array
{
  add(val)
  {
    this.unshift(val);
  }
  remove()
  {
    this.shift();
  }
}

class MyClass 
{
  constructor() 
  {
    this.date_created = new Date()
    
    this.post = new Post();
  }
}
let x = new MyClass();
console.log(x.post);
x.post.add(2);
console.log(x.post);

added 596 characters in body
Source Link
Eddie D
  • 1.1k
  • 7
  • 16

If you want to keep the actual array hidden and untouchable except through the methods, it has to be declared in the constructor. Any function that alters it has to be declared in the constructor as well. If that item is to be publicly accessible, it has to be attached to this.

UPDATE : Fixed Typo

class MyClassPost extends Array
{
  constructoradd(val) 
  {
    var items = [];
    this.date_createdunshift(val);
 = new}
 Date remove(val)
    {
    this._posts = shift(val);
  }
}

class MyClass  
{
  constructor()  
  {
 add : function(val) { itemsthis.unshiftdate_created = new Date(val); },
     
  remove: function(val) { itemsthis.shiftpost = new Post(val); }
    }
  }
let x get= postnew MyClass() { return this;
console._posts; }log(x.post);
}x.post.add(2);
console.log(x.post);

If you want to keep the actual array hidden and untouchable except through the methods, it has to be declared in the constructor. Any function that alters it has to be declared in the constructor as well. If that item is to be publicly accessible, it has to be attached to this.

UPDATE : Fixed Typo

class MyClass 
{
  constructor() 
  {
    var items = [];
    this.date_created = new Date()
    
    this._posts = 
    {
      add : function(val) { items.unshift(val); },
      remove: function(val) { items.shift(val); }
    }
  }
  get post() { return this._posts; }
}

If you want to keep the actual array hidden and untouchable except through the methods, it has to be declared in the constructor. Any function that alters it has to be declared in the constructor as well. If that item is to be publicly accessible, it has to be attached to this.

class Post extends Array
{
  add(val)
  {
    this.unshift(val);
  }
  remove(val)
  {
    this.shift(val);
  }
}

class MyClass  
{
  constructor()  
  {
    this.date_created = new Date()
     
    this.post = new Post();
  }
}
let x = new MyClass();
console.log(x.post);
x.post.add(2);
console.log(x.post);

deleted 6 characters in body
Source Link
Eddie D
  • 1.1k
  • 7
  • 16

If you want to keep the actual array hidden and untouchable except through the methods, it has to be declared in the constructor. Any function that alters it has to be declared in the constructor as well. If that item is to be publicly accessible, it has to be attached to this.

UPDATE : Fixed Typo

class MyClass 
{
  constructor() 
  {
    var items = [];
    this.date_created = new Date()
    
    this.posts_posts = 
    {
      get: function { return [...items]; },
      add : function(val) { items.unshift(val); },
      remove: function(val) { items.shift(val); }
    }
  }
  get post() { return this._posts; }
}

If you want to keep the actual array hidden and untouchable except through the methods, it has to be declared in the constructor. Any function that alters it has to be declared in the constructor as well. If that item is to be publicly accessible, it has to be attached to this.

UPDATE : Fixed Typo

class MyClass 
{
  constructor() 
  {
    var items = [];
    this.date_created = new Date()
    
    this.posts = 
    {
      get: function { return [...items]; },
      add : function(val) { items.unshift(val); },
      remove: function(val) { items.shift(val); }
    }
  }
}

If you want to keep the actual array hidden and untouchable except through the methods, it has to be declared in the constructor. Any function that alters it has to be declared in the constructor as well. If that item is to be publicly accessible, it has to be attached to this.

UPDATE : Fixed Typo

class MyClass 
{
  constructor() 
  {
    var items = [];
    this.date_created = new Date()
    
    this._posts = 
    {
      add : function(val) { items.unshift(val); },
      remove: function(val) { items.shift(val); }
    }
  }
  get post() { return this._posts; }
}

Source Link
Eddie D
  • 1.1k
  • 7
  • 16
Loading