Home > other >  How to access to a class's property without an instance?
How to access to a class's property without an instance?

Time:01-02

Currently, I have a class of this sort:

class MyClass {
  constructor(c) {
    this.a = "a";
    this.b = "b";
  }

  myMethod() {
    return c;
  }
}

As you can see c would be a property that I want to be returned when I execute a method on an instance, but it is not in the object instance.

Private properties would not work, because if I stringify the object, the property is also in the string, and I don't want it there.

Is there any way to achieve this? Not necessarily a complete solution but some hints would be enough.

CodePudding user response:

Private properties would not work, because if I stringify the object, the property is also in the string

No it's not? This works fine:

class MyClass {
  #c;
  constructor(c) {
    this.a="a";
    this.b="b";
    this.#c=c;
  }
  myMethod() {
    return this.#c;
  }
}
const obj = new MyClass('hi');
console.log(JSON.stringify(obj));
console.log(obj.myMethod());

An alternative would be to create the method in the constructor, as a closure over the c variable:

class MyClass {
  constructor(c) {
    this.a="a";
    this.b="b";
    this.myMethod = () => {
      return c;
    };
  }
}
const obj = new MyClass('hi');
console.log(JSON.stringify(obj));
console.log(obj.myMethod());

Further alternatives that work with normal properties and prevent inclusion in the JSON.stringify result are to make the c property non-enumerable or to define a custom toJSON method.

CodePudding user response:

In my real world class I added the properties:

class MyClass{

  //other props
  preQ: string;
  postQ: string;

  constructor(data: InputData = { cli: cli.flags }) {
    Object.defineProperties(this, {
      preQ: { enumerable: false },
      postQ: { enumerable: false },
    });
    // other stuff
 }
}

As indicated to in the first comment by Pointy. The properties are not present in the JSON.stringify result.

Which are properties that I do not want to be sent to the server.

CodePudding user response:

Use static Keyword :

MDN Documentation :

Static properties cannot be directly accessed on instances of the class. Instead, they're accessed on the class itself.

Static methods are often utility functions, such as functions to create or clone objects, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.

Example

class Student {
  name: string;
  static age: number;
  constructor(age: number) {
    this.name = 'Jhon';
    Student.age = age;
  }
  static getAge = () => Student.age;
}

const student = new Student(20);

const json = JSON.stringify(student); // {"name":"Jhon"}
console.log(json);
console.log(Student.getAge()); // 20

Your code:

class MyClass {
  a: string;
  b: string;
  static c: string;
  constructor(c:string) {
    this.a = 'a';
    this.b = 'b';
    MyClass.c = c;
  }
  myMethod() {
    return MyClass.c;
  }
}
const obj = new MyClass('hi'); 
console.log(JSON.stringify(obj)); // {"a":"a","b":"b"}
console.log(obj.myMethod()); // hi

  • Related