1

I knew there is a lot of articles about this theme, but I wanna know about my code, why this.getNewsFeedLook() Is it not a function?

define(["jquery"], function ($) {

    let objectNews = {

        title: undefined,
        type: undefined,
        img: undefined,
        link: undefined,
        content: undefined,
        newsFeedLook: this.getNewsFeedLook(),

        getNewsFeedLook: function () {
            let html = "" +
                "<div class='col-xl-3 col-lg-4 col-md-6 col-sm-6 col-12'>" +
                "   <div class='newsbox'>" +
                "       <a href='" + this.link + "'>" +
                "           <img src='" + this.img + "'>" +
                "           <div class='dark_folie'>" +
                "               <p>" + this.title + "</p>" +
                "               <span>" + this.type + "</span>" +
                "</div></a></div></div>";

            return html;
        },

    };
    return objectNews;
});
Minhaj Patel
  • 581
  • 1
  • 5
  • 19

2 Answers2

0

Try defining attribute newsFeedLook after getNewsFeedLook. It should solve your problem.

0

The reason you are getting this.getNewsFeedLook() is it not a function is because the this is not referring to objectNews scope (which you are expecting).

The simple fix is to create newsFeedLook a function. Your object will look like this then:

let objectNews = {

    title: undefined,
    type: undefined,
    img: undefined,
    link: undefined,
    content: undefined,
    newsFeedLook: function(){ return this.getNewsFeedLook()},

    getNewsFeedLook: function () {
        let html = "" +
            "<div class='col-xl-3 col-lg-4 col-md-6 col-sm-6 col-12'>" +
            "   <div class='newsbox'>" +
            "       <a href='" + this.link + "'>" +
            "           <img src='" + this.img + "'>" +
            "           <div class='dark_folie'>" +
            "               <p>" + this.title + "</p>" +
            "               <span>" + this.type + "</span>" +
            "</div></a></div></div>";

        return html;
    },

};
Zain Zafar
  • 1,557
  • 4
  • 13
  • 23