1

How to detect whether EcmaScript class has its own constructor? See code snippet below.

class Person {
  constructor(name, age) {   // <- Own constructor
    this._name = name;
    this._age = age;
  }
}


class Manager extends Person {
    // This class does not have it's own constructor
}

It seems there is no clear way to do this check.

Thank you all for time and help.

4
  • 1
    Can you elaborate on what it means to have "its own constructor"? You do know that classes aren't real, right, but only syntactic sugar for functions and their prototypes? Commented Sep 8, 2017 at 20:05
  • @RayToal, see example. I need to figure out somehow that class Person has its own constructor and class Manager don't. Commented Sep 8, 2017 at 20:19
  • Can you elaborate on why you want to do this? It seems like what you are asking is to have access to the source code. In JavaScript, when you say class and constructor you are NOT making classes and constructors Instead you are defining a function and maybe adding things to its prototype. I really don't see how this question (while interesting) has a relevant answer, because classes aren't real in JavaScript. Are you asking whether at run time you can see what the source code looked like? I think it is impossible without major hacks.... Commented Sep 11, 2017 at 2:17
  • @RayToal, you are right, there's no way to do what I want. I'm working on DI mechanism and needed to get constructor of the class. But unfortunately, constructor method === class definition, so I should find another way to do what I need. Commented Sep 12, 2017 at 7:31

2 Answers 2

2

It's not bulletproof, but you could convert the constructor to a string, and see if it contains the word constructor( or something similar

function hasOwnConstructor(instance) {
    var str = instance.constructor.toString();
    return /class(.*?[^\{])\{([\s\n\r\t]+)?(constructor[\s]+?\()/.test(str);
}

class thing1 { 
    method() { return this; }
}

class thing2 { 
    constructor(argument) { return this; }
}

var ins1 = new thing1();
var ins2 = new thing2();

console.log( hasOwnConstructor(ins1) ); // false
console.log( hasOwnConstructor(ins2) ); // true

False positives could still be possible, but the regex makes the check quite strict, so it's not very plausable.

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, but I still need 100% solution.
This seems like a programmatic solution to determining whether or not a class contains a constructor
@AaronEverly, yes, but it's a weak check. Thare can be some commented code that contains fragment constructor( or something else, anything... Thats why this solution is not 100% safe.
@AlexChandler maybe create a regular expression or function that searches for the exact schema of the constructor function. Something that detects a class declaration and then looks for the constructor. Are you looking for a solution to parse rendered HTML?
@AlexChandler - I don't think there is one. There is no method that will tell you if a constructor exists, what it is etc. the best I think you can do is get the class as a string, and try to figure it out from that.
|
0

Inspect the constructor function.

class Example {

    constructor(prop1, prop2) {

    this.prop1 = prop1;
    this.prop2 = prop2;

      }
    }

You can pass parameters to your class via the constructor function that establishes properties which can be globally referenced when creating a new instance.

This line would pass int 1 and "test" as values to prop1 and prop2:

const exampleVar = new Example(1, "test");

and might render something like:

<Example prop1=1 prop2="test" />

Hope this helps. If it has a constructor function, there is a constructor, if it does not have the constructor function, then there is no constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.