无法从 JS 中的简单模块调用方法
cannot call method from a simple module in JS
我正在尝试从我的模块 (Tweeter) 中获取要传递的数组 (_posts),
通过在调用时使用 getPosts 函数 return 它。
当我尝试打印 it/pass 它时 - 它只是打印函数的代码而不是 return 数组。
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的Object,如
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();
希望对您有所帮助。
我正在尝试从我的模块 (Tweeter) 中获取要传递的数组 (_posts), 通过在调用时使用 getPosts 函数 return 它。
当我尝试打印 it/pass 它时 - 它只是打印函数的代码而不是 return 数组。
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的Object,如
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();
希望对您有所帮助。