如何在对象方法中选择JavaScript对象数组

时间:2015-10-07 18:32:13

标签: javascript arrays javascript-objects

如何从randomImage.imagePicker方法中选择randomImage.images数组中的字符串?

(function() {

var randomImage = {

        images : [
                'http://placehold.it/100x100',
                'http://placehold.it/200x200',
                'http://placehold.it/300x300',
                'http://placehold.it/400x400'
                ],//images Array
        imagePicker : function () {
                return console.log(randomImage.images[2]);
                }()//imagePicker Method

}//randomImage Object

})()

我在控制台中收到以下错误:

  

未捕获的TypeError:无法读取属性'图像'未定义的

4 个答案:

答案 0 :(得分:1)

或者,如果要指定要获取的randomImage的索引:

imagePicker : function (idx) {
  return randomImage.images[idx];
}

randomImage.imagePicker(1)将返回http://placehold.it/200x200

答案 1 :(得分:1)

  

我在控制台中收到以下错误:Uncaught TypeError: Cannot read property 'images' of undefined

问题是你在函数定义之后有()

imagePicker : function () {
    return console.log(randomImage.images[2]);
}()//imagePicker Method
 ^^

这将立即调用函数并将其返回值赋给imagePicker。但此时randomImage尚未初始化。它的值仍然是undefined,这就是你得到错误的原因。

删除括号以指定函数本身(我假设这是你想要做的):

imagePicker : function () {
    return console.log(randomImage.images[2]);
}

答案 2 :(得分:0)

如果您只是希望它从images数组中返回一个随机图像:

DEMO

imagePicker : function () {
  var index = Math.floor(Math.random() * randomImage.images.length);
  return randomImage.images[index];
}

只需使用索引即可返回特定图像:

imagePicker : function () {
  // index 0 returns 'http://placehold.it/100x100'
  return randomImage.images[0];
}

答案 3 :(得分:0)

var lookup_arr = new Array(); lookup_arr['XXX'] = ' bunch of Xs '; lookup_arr['YYY'] = ' bunch of Ys '; function addMoreRows(frm) { var recRow = '<tr><td width="200px;"><input /></td> <td width="200px;"><div></div></td> <td> <a href="javascript:void(0);" onclick="removeRow(this);">Delete this Row</a> </td> </tr>'; //add new row and return the jQuery object for it var tr = $('#addedRows').append(recRow).find('tr:last'); //collection of td in the newly added row var tds = tr.find('td'); //find dynamically added input var input = tds.eq(0).find('input'); //set value for new item based off previous rows input value var prevInput = tr.prev().find('td').eq(0).find('input'); input.val(prevInput.val()); //add description to item based off input value tds.eq(1).html(lookup_arr[input.val()]); //add handler to update description based off input value input.on('input paste', function () { $this = $(this); var exists = lookup_arr[this.value] if (exists) { $this.closest('tr').find('td').eq(1).html(lookup_arr[this.value]) } else { $this.closest('tr').find('td').eq(1).html('not found') } }) } function removeRow(el) { $(el).closest('tr').remove(); } 是一个神奇的词:

this

请注意, (function() { var randomImage = { images : [ 'http://placehold.it/100x100', 'http://placehold.it/200x200', 'http://placehold.it/300x300', 'http://placehold.it/400x400' ],//images Array imagePicker : function () { alert(this.images[2]); }//imagePicker Method }//randomImage Object randomImage.imagePicker(); })(); 变量在自动功能之外不可用!

小提琴here