// person.js
'use strict';
module.exports = class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
display() {
console.log(this.firstName + " " + this.lastName);
}
}
// index.js
'use strict';
var Person = require('./person.js');
var someone = new Person("First name", "Last name");
someone.display();
Answer from sitrakay on Stack OverflowVideos
// person.js
'use strict';
module.exports = class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
display() {
console.log(this.firstName + " " + this.lastName);
}
}
// index.js
'use strict';
var Person = require('./person.js');
var someone = new Person("First name", "Last name");
someone.display();
If you are using ES6 in Node 4, you cannot use ES6 module syntax without a transpiler, but CommonJS modules (Node's standard modules) work the same.
module.export.AspectType
should be
module.exports.AspectType
hence the error message "Cannot set property 'AspectType' of undefined" because module.export === undefined.
Also, for
var AspectType = class AspectType {
// ...
};
can you just write
class AspectType {
// ...
}
and get essentially the same behavior.
You are only importing the class, but not making an instance of the class
Try
var myInstance = new Example()
myInstance.test()
If you want to call a method as a class method (without creating an object instance) you can try static methods.
You can change the file2.js as,
export default class Example {
static test() {
console.log('hello world');
}
}
then call it by using the class name in file1.js as
import Example from './file2';
console.log(Example.test());
Refer James Maa answer if you want to call it as an instance method.
You would have to export it on the prototype. But remember that if you do that you won't call the function in the class/object context:
export foo.prototype. fooMethod
However I would recommend you to not to do so.
Okay, due to your comment you want a good way to have a common functionality for two classes, that don't extend the same base class. One simple way is to import a utility function from two classes:
foo.js
export function foo() {
return this.name;
}
a.js
import {foo} from 'foo';
export class A extends BaseA {
foo() {
foo.apply(this, arguments);
}
}
b.js
import {foo} from 'foo';
export class B extends BaseB {
foo() {
foo.apply(this, arguments);
}
}
This is a good pattern and works well for a single function, but has limits if you want to apply more complex functionality. A good way to achieve this is a mixing pattern:
foo.js
export default superClass => class extends superClass {
foo() {
return this.name;
}
};
a.js
import foo from 'foo';
export class A extends foo(BaseA) {
..
}
b.js
import foo from 'foo';
export class B extends foo(BaseB) {
..
}
This will make your mixing create a new anonymous class between your class 'A'/'B' and 'BaseA'/'BaseB', which provides the common function foo.
You can export and import class methods by instantiating the class which obviously turns it into an object and then exporting each method by destructuring them from the new instantiated object check code example below.
Destruct and export object methods like this:
class foo {
doSomething() {
// some stuffs
}
doAnotherThing() {
// do something else
}
}
export const { doSomething, doAnotherThing } = new foo()
Then in your file where you want to import the methods do this:
import { doSomething, doAnotherThing } from '../class/file/directory'
doSomething() // calls the method
I hope this helps