Javascript “我为什么收到这封信”;this.getNewsFeedLook不是一个函数";,当这是一个函数时

Javascript “我为什么收到这封信”;this.getNewsFeedLook不是一个函数";,当这是一个函数时,javascript,function,object,Javascript,Function,Object,我知道有很多关于这个主题的文章,但是我想知道我的代码,为什么this.getNewsFeedLook()它不是一个函数吗 define(["jquery"], function ($) { let objectNews = { title: undefined, type: undefined, img: undefined, link: undefined, content: undefined,

我知道有很多关于这个主题的文章,但是我想知道我的代码,为什么
this.getNewsFeedLook()
它不是一个函数吗

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;
});
define([“jquery”]、函数($){
让objectNews={
标题:未定义,
类型:未定义,
img:未定义,
链接:未定义,
内容:未定义,
newsFeedLook:this.getNewsFeedLook(),
getNewsFeedLook:函数(){
让html=“”+
"" +
"   " +
"       ";
返回html;
},
};
返回目标新闻;
});

尝试在getNewsFeedLook之后定义属性
newsFeedLook
。它应该可以解决您的问题。

尝试在getNewsFeedLook之后定义属性
newsFeedLook
。它应该可以解决您的问题。

您之所以得到
this.getNewsFeedLook()不是一个函数
是因为
this
不是指
objectNews
范围(您期望的范围)

简单的修复方法是创建一个函数。您的对象将如下所示:

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;
    },

};
let objectNews={
标题:未定义,
类型:未定义,
img:未定义,
链接:未定义,
内容:未定义,
newsFeedLook:function(){返回this.getNewsFeedLook()},
getNewsFeedLook:函数(){
让html=“”+
"" +
"   " +
"       ";
返回html;
},
};

您之所以得到
this.getNewsFeedLook()不是一个函数
是因为
this
不是指
objectNews
范围(您期望的范围)

简单的修复方法是创建一个函数。您的对象将如下所示:

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;
    },

};
let objectNews={
标题:未定义,
类型:未定义,
img:未定义,
链接:未定义,
内容:未定义,
newsFeedLook:function(){返回this.getNewsFeedLook()},
getNewsFeedLook:函数(){
让html=“”+
"" +
"   " +
"       ";
返回html;
},
};