0

How do I properly call the functions inside pretest?

I get this error: Uncaught TypeError: b.testmenow is not a function

    var pretest = function () {
        var MAX_NUM = 250.0;
    
        var prebase = function (NEW_NUM) {
            this.NEW_NUM = NEW_NUM ? NEW_NUM : true;
        };
    
        prebase.prototype.testmenow = function () {
            return this.NEW_NUM;
        };
        
        return prebase;
    };
    
    var b = new pretest(111);
    console.log(b.testmenow());

4
  • 1
    Part of me says you need to return new prebase() but it's super strange how you're doing this. Try searching to learn more on prototypes and constructors. Commented Jul 3, 2018 at 0:56
  • how do i fix the code? Commented Jul 3, 2018 at 0:58
  • 1
    pretest is a function that returns a constructor, it's not a constructor itself. Not sure why you are doing this, but with minimal changes to your code: var b = new (pretest())(111);. Commented Jul 3, 2018 at 1:00
  • Curious to know why you don't just use prebase directly — what is pretest's contribution? Commented Jul 3, 2018 at 1:04

1 Answer 1

1

You need to accept your input into new pretest(111) by adding n. And then you must instantiate your prebase constructor using n.

    var pretest = function (n) {
        var MAX_NUM = 250.0;
    
        var prebase = function (NEW_NUM) {
            this.NEW_NUM = NEW_NUM ? NEW_NUM : true;
        };
    
        prebase.prototype.testmenow = function () {
            return this.NEW_NUM;
        };
        
        return new prebase(n);
    };
    
    var b = pretest(111);
    console.log(b.testmenow());

It is strange that you have two constructors here, you can surely do this with one.

As Felix has deftly mentioned, you can call pretest(111) instead of new pretest(111).

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

2 Comments

There is no reason to call pretest with new. It's rather confusing. Just do var b = pretest(111);.
so If I have multiple parameters I need to return new prebase(n,x,y,z); ?

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.