2017-08-31 149 views
0

道歉,但无法找到此问题的答案。我在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>

+4

你的外部函数没有return语句。 – david25272

+0

此外itemsObj只会在用户单击.info元素后才会填充,而不是在调用getItem函数时调用 – Frankusky

+0

尝试从事件处理函数返回值没有任何意义。它应该返回到哪里?不是你调用事件处理程序,而是事件发生时的浏览器。 –

回答

1

它不返回任何东西那是因为你返回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 
}); 

提示:如果你想办法解决冷却器要实现successfail场景检查jQuery DefferedJavaScript Promises

希望帮助

+0

感谢您对Masterpreenz的帮助。将查看您的解决方案和您提供的链接。 – Lucky500

1

,因为你是从一个事件处理程序是另一个函数本身返回。 如果您只想将它​​推送到本地存储,为什么不在事件处理程序中执行?

$('.info').on('click', function(){ 
 
    itemsObj = {}; 
 
    $this = $(this); 
 
    itemsObj.gameImg = $this.parent().parent().find('img').attr('src'); 
 
    itemsObj.gameTitle = $this.parent().parent().find('h2').text(); 
 
    itemsObj.gameInfo = $this.parent().parent().find('p').text(); 
 
    // getting previous info 
 
    var savedInfo = localStorage.getItem("gamesinfo"); 
 
    // since everything is saved as string , we need to use json 
 
    savedInfo = JSON.parse(savedInfo); 
 
    // check if its not null 
 
    savedInfo = savedInfo ? savedInfo : {}; 
 
    // use a unique identifier to save objects , do not push the object into an array every time 
 
    savedInfo[itemsObj.gameTitle] = itemsObj; 
 
    // stringify data and save it 
 
    localStorage.setItem("gamesinfo", JSON.stringify(savedInfo)); 
 
    
 
    
 
    
 
    // to retrieve all of games info: 
 
    console.log(JSON.parse(localStorage.getItem("gamesinfo"))); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<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 .....</p> 
 
     i<a href="#" class="info">♥</a> 
 
    </div> 
 
</div>

+0

感谢精心解答Javad。但仍然没有回答我的问题,我希望这一切都在一个函数中,如果我能够控制日志对象并查看属性,那么当我返回未定义的函数时如何? – Lucky500

+0

如果你仔细观察,你已经为.on点击定义了另一个函数,并且你只是返回该函数,所以你的getItem函数不会返回任何东西,这就是为什么你没有定义。你应该在返回时检查最近的函数定义,在你的情况下,你已经定义了它的onclick函数,而不是getItem。希望能帮助到你。 – Javad