не может вызвать метод из простого модуля в JS - PullRequest
0 голосов
/ 19 апреля 2020

Я пытаюсь получить массив (_posts) для передачи из моего модуля (Tweeter), используя функцию 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
  }

}

1 Ответ

0 голосов
/ 19 апреля 2020

После определения вам нужно создать новый объект твитера, например

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();

Надеюсь, это поможет.

...