0

I am new to nodejs and JavaScript. I am writing automation framework using pageobject model using webdriver.io v5. I am facing two issues and would appreciate some clarification.

****************** Question 1 ******************
I have the following format code which works fine but returns Undefined as return value in main class

class ABC.js
***************
class ABC {
   constructor() {};
   extractCode() {
     return (1 > 2).valueOf();// I am expecting false as output
   }
}
module.exports = ABC;
*********** class D.js *******

var ABC = require('ABC');
class D extends ABC {
   const abc = new ABC();
   console.log(abc.extractCode());// I get undefined
}
************** Question 2 ************
    class ABC.js
    ***************
    class ABC {
       constructor() {};
       function extractCode() {
         return (1 > 2).valueOf();// I am expecting false as output
       }
    }
    module.exports = ABC;

I get the following error :

function extractCode() {
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

SyntaxError: Unexpected identifier

Why is adding function keyword throwing error on JavaScript?

2
  • You're not closing the Function Commented Mar 13, 2020 at 3:28
  • @tramada Updated the code. Thanks for letting me know Commented Mar 13, 2020 at 3:47

2 Answers 2

2

Question 1:

return (1 > 2).valueOf();// I am expecting false as output

Just return 1 > 2;. The expression already evaluates to a boolean value. No need for valueOf().


Question 2:

You're declaring a method on a class. So this is valid:

class A {
  extractCode () {
   // do stuff
  }
}

But what you're doing in your code is trying to declare a freestanding function in the context of a class definition, which isn't valid js.


Observation

Why are you instantiating ABC inside a class that extends ABC?

class D extends ABC {
   const abc = new ABC(); // why?
   console.log(abc.extractCode());// I get undefined
}

class ABC {
  extractCode () {
    return 1 > 2;
  }
}

class DEF extends ABC {} // inherits extractCode from ABC

class GHI extends DEF {
  extractCode () { // override to change the subclass behavior
    return 'bananas';
  }
}

const abc = new ABC();
console.log(abc.extractCode()); // false

const def = new DEF();
console.log(def.extractCode()); // false

const ghi = new GHI();
console.log(ghi.extractCode()); // bananas

These questions are more about fundamental javascript syntax than all the technologies you tagged it with. This has nothing at all to do with jquery, node.js, reactjs, or angular-fullstack.

Read up on the basics of javascript.

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

2 Comments

I tried that return 1 >2 and I get Undefined on main class
I am still learning class patters in JS. I am from Java background. But to answer your question on why I am using class declaration, I need to use the methods from ABC into class D which should give me value false. I have declared require('ABC') before the class definition of D and instantiated ABC inside class D. I know this might be wrong, still trying to learn.
0

For question 2: It should just be extractCode(){}, remove the function keyword. This is because you don't need it when declaring class methods. Also add a closing bracket.

Try this:

    class ABC {
       constructor() {};
       extractCode() {
         return (1 > 2).valueOf();
       }
    }
    module.exports = ABC;

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.