0

Hi I have the following class definition:

'use strict'


class Human {
  constructor(name, age) {
    this._name = name || null;
    this.age = age || 'no age defined';
    this.rights = ['Human Rights'];
  }

  get this() {
    return 'access denied';
  }

  set name(name) {
    this._name = this._name ? this._name : name;
  }

  get name() {
    return this._name;
  }
}

let me = new Human();

console.log(me); // this should return a string "access denied"

I thought it might be possible to define a getter for the whole instance. But this is not, or? Does anyone know anything about that? Or is there an alternative methods to create restricted Classes?

kind regards martin

marschro
  • 791
  • 8
  • 23
  • 2
    I think here the getter is set for a member `this` inside `me` and does not refer the `me` itself. If you try `me['this']` you'll get "access denied" – Vivek Sep 18 '15 at 10:40
  • 2
    When you `new` a class you are calling its `constructor` not the `get this()` (which is not a reserved ES6 class method like `constructor`). – sdgluck Sep 18 '15 at 10:41
  • 2
    A getter for the whole instance *is* the *constructor*. Can you specify what you mean by a *restricted class*? – CodingIntrigue Sep 18 '15 at 11:02
  • @RGraham thanks for this hint. With restricted I meant, that when you "call" the instance of a class like console.log(me) you must not get the whole instance with all it's properties ({_name:'foo', _age:bar}), but only an error or some string. So one can't see the internal properties of the instance. I don't know if this makes sense at all, I just wondered if it's possible,... – marschro Sep 18 '15 at 11:55
  • 2
    What purpose would that serve? If you create an object that shouldn't be accessed, why to you create / expose it at all? – Felix Kling Sep 18 '15 at 13:43
  • I wanted to make a Class that, when its instance is called, it does not return the internal underscored variables, but only those that are accessible by setters and getters. – marschro Sep 18 '15 at 14:18
  • @marschro: Why would you? You can achieve this by not creating underscrored properties in the first place (and using closures instead), but there's no reason to do so. – Bergi Sep 18 '15 at 15:50

1 Answers1

0

No, but what you could do is put some code in a module and only export part of it.

 // human.js                                                     
 class SecretHuman {                                            
   constructor(name) {                                          
     this.name = name;                                          
   }                                                            
 }                                                              
 let human = new SecretHuman('Bob');                            
 export class Human {                                           
   secret() {                                                   
     console.log(`Secret human is ${human.name}`);              
   }                                                            
 }                                                              

// test.js 
import {Human} from './human';
new Human().secret();
Jason Livesay
  • 6,317
  • 3
  • 25
  • 31