Reputation: 69785
I am almost new to JavaScript, and I am trying to access an object within a class. This is my class definition in a file called analysis.po.js:
var AnalysisPage = function () {
(some code here)
this.getSpousesCreditBureau = function() {
return {
pdScore: getSpousesCreditBureauElement('pdScore'),
qualification: getSpousesCreditBureauElement('qualification'),
estimatedFee: getSpousesCreditBureauElement('estimatedFee'),
currentDebt: getSpousesCreditBureauElement('currentDebt'),
maxDebt: getSpousesCreditBureauElement('maxDebt'),
arrears: getSpousesCreditBureauElement('arrears'),
segment: getSpousesCreditBureauElement('segment'),
bpGlobalRisk: getSpousesCreditBureauElement('bpGlobalRisk'),
groupGlobalRisk: getSpousesCreditBureauElement('groupGlobalRisk')
};
};
(some other code here)
};
module.exports = new AnalysisPage();
This is the piece of code where I try to get the object getSpousesCreditBerauElement
in another file called analysis.spec.js:
var App = require('../app.po.js'),
Util = require('../util.js'),
AnalysisPage = require('./analysis.po.js'),
AnalysisData = require('./analysis.data.js');
(some code here)
var analysis = new AnalysisPage();
Util.verifyElementsAreDisplayed(analysis.getSpousesCreditBureau());
(some other code here)
The error I am getting is: Cannot call method 'getSpousesCreditBureau' of undefined
Upvotes: 0
Views: 47
Reputation: 57729
You're not actually exporting AnalysisPage
and you're not calling it correctly.
Export the class with:
module.exports = AnalysisPage;
In comparison
module.exports = new AnalysisPage();
Exports an instance of the class.
The right way to call it is then:
var instance = new AnalysisPage();
Util.verifyElementsAreDisplayed(instance.getSpousesCreditBureau());
(Original question has been modified, code was:)
var analysis = new AnalysisPage();
Util.verifyElementsAreDisplayed(AnalysisPage.getSpousesCreditBureau());
You can export just the instance, in that case call it like:
var instance = require('./analysis.po.js');
Util.verifyElementsAreDisplayed(instance.getSpousesCreditBureau());
So no new
anywhere.
Upvotes: 2
Reputation: 21465
Did you tried:
var analysis = new AnalysisPage();
Util.verifyElementsAreDisplayed(analysis.getSpousesCreditBureau());
When you access the method like this AnalysisPage.getSpousesCreditBureau()
you are not accessing the instance, but the class definition.
Upvotes: 1