Export commonLogic functions to external javascript class/function


#1

I am trying to move some common logic from multiple of my models in NativeScript to one seperate file that I want to require in my models. I don’t have any Javascript experience and wanted to make a class like you’d do in Java however this appearantly doesn’t work.

Can anybody explain what goes wrong and how I’d have to go about this?

This is what I currently have (example code):

CommonLogic.js

function Person(lastName, firstName)
{
this.firstName = firstName;
this.lastName = lastName;
}

Model1.js

var Person = require("~/CommonLogic");

exports.loaded = function(args) {
var person = new Person(‘Clouseau’, ‘Jacques’);
console.log('The person is ’ + person);
}

Error:

System.err: Calling js method onCreateView failed
System.err:
System.err: TypeError: Person is not a constructor

Thanks in advance!

EDIT:
Answered on stackoverflow: https://stackoverflow.com/questions/49879591/javascript-class-function-in-nativescript