javascript - 尝试从函数返回一个对象

标签 javascript jquery object

抱歉,但无法找到此问题的答案。我正在 html 上查找项目并将其推送到一个对象,但我无法弄清楚为什么这个函数不返回该对象?我可以毫无问题地进行安慰。

function getItem(obj){
  var itemsObj;
  $('.info').on('click', function(){
    itemsObj = {};
    $this = $(this);
    itemsObj.gameImg = $this.parent().parent().find('img').attr('src');
    itemsObj.gameTitle  = $this.parent().parent().find('p').text();
    itemsObj.gameInfo = $this.parent().parent().find('h2').text();
    // console.log(itemsObj);
    return itemsObj;
  });
}
//getItem();
console.log(getItem());
<div class="col-lg-3 game">
              <div class="view view-first">
               <img src="img/deathstranding.jpg"/>
               <div class="mask">
               <h2>Death Stranding</h2>
               <p>Death Stranding is an upcoming action video game developed by Kojima Productions and published by Sony Interactive Entertainment for PlayStation 4. It is the first game by game director Hideo Kojima and his company following the 2015 disbandment of Kojima Productions as a subsidiary of Konami and subsequent reformation as an independent studio.</p>
                   i<a href="#" class="info">♥</a>
               </div>
             </div>

最佳答案

它没有返回任何内容的原因是因为您返回了 click 事件监听器函数的数据而不是 getItem() 函数,也没有办法返回就这样吧

function getItem(obj){
  var itemsObj;
  $('.info').on('click', function(){
    itemsObj = {};
    $this = $(this);
    itemsObj.gameImg = $this.parent().parent().find('img').attr('src');
    itemsObj.gameTitle  = $this.parent().parent().find('p').text();
    itemsObj.gameInfo = $this.parent().parent().find('h2').text();
    // This sends data to click event listener not getItem()
    return itemsObj;
  });
}

为了接收您想要的项目,您应该实现回调

function getItem(callback){
  var itemsObj;
  $('.info').on('click', function(){
    itemsObj = {};
    $this = $(this);
    itemsObj.gameImg = $this.parent().parent().find('img').attr('src');
    itemsObj.gameTitle  = $this.parent().parent().find('p').text();
    itemsObj.gameInfo = $this.parent().parent().find('h2').text();
    // call the callback function parameter and send itemsObj as argument, callback function then received the argument as you wanted it to be. Then execute stuff from there.
    callback(itemsObj);
  });
}

// send a function instead for getItem to call when all is well
getItem(function (data) {
   // here you will receive the data
   console.log(data);
   // stuff
});

提示:如果您想要一个更酷的解决方案来实现成功失败场景,请检查 jQuery Deffered JavaScript Promises

希望有帮助

关于javascript - 尝试从函数返回一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45971902/

相关文章:

javascript - 如何在 JavaScript 中使用键值作为变量名?

c++ - 无法删除和删除 vector 上的对象指针

javascript - 遍历具有相同类的动态生成的 HTML 元素,并使用它们的数据构建对象

javascript - 如何确保在执行脚本之前包含某些库?

javascript - 默认情况下, Shiny 的应用程序数据对于特殊输入小部件不可见

javascript - Facebook Like 按钮未显示在 fancybox 标题中

javascript - 使用另一个属性值更改数组中对象的属性值

javascript - 如果空格出现在 JavaScript 中的两个字符之间,我该如何跳过空格?

asp.net - 使用 ScriptManager 而不是仅使用客户端 &lt;script&gt; 标记有什么好处?

jquery - 当使用参数更新 URL 时,Pjax 后退按钮不起作用