После определения вам нужно создать новый объект твитера, например
const tweets = new Tweeter();
// now you can do
tweets.getPosts();
, или вы можете просто вызвать определение вашей функции, например:
const Tweeter = function() {
const _posts = [{
text: "First post!",
id: "p1",
comments: [{
id: "c1",
text: "First comment on first post!"
},
{
id: "c2",
text: "Second comment on first post!!"
},
{
id: "c3",
text: "Third comment on first post!!!"
}
]
},
{
text: "Aw man, I wanted to be first",
id: "p2",
comments: [{
id: "c4",
text: "Don't wory second poster, you'll be first one day."
},
{
id: "c5",
text: "Yeah, believe in yourself!"
},
{
id: "c6",
text: "Haha second place what a joke."
}
]
}
]
const getPosts = function() {
return _posts
}
return {
getPosts: getPosts
}
}();
Теперь вы можете просто сделать, Tweeter.getPosts ();
А если вы экспортируете это из файла, вы можете просто сделать:
export const Tweeter = function() {
const _posts = [{
text: "First post!",
id: "p1",
comments: [{
id: "c1",
text: "First comment on first post!"
},
{
id: "c2",
text: "Second comment on first post!!"
},
{
id: "c3",
text: "Third comment on first post!!!"
}
]
},
{
text: "Aw man, I wanted to be first",
id: "p2",
comments: [{
id: "c4",
text: "Don't wory second poster, you'll be first one day."
},
{
id: "c5",
text: "Yeah, believe in yourself!"
},
{
id: "c6",
text: "Haha second place what a joke."
}
]
}
]
const getPosts = function() {
return _posts
}
return {
getPosts: getPosts
}
};
И теперь вы можете импортировать в другие файлы, такие как:
import {Tweeter} from './tweeter.js';
// use it like
const tweets = new Tweeter();
const posts = tweets.getPosts();
Надеюсь, это поможет.