我目前正在研究javascript标准编码模式。为什么有人console.dir(dropdownAC.appendImages())
返回undefined?
在这段代码中,我使用dev-bridge autocomplete.js拥有一个自动完成小部件,例如homedepot autocomplete,其中有一个图片框建议。如果即时通讯做错了,请随时提出我需要使用的正确模块化格式。目前,图像将位于本地存储中。
(function ($) {
"use strict";
var DropdownAC = function() {
return this
}
DropdownAC.DEFAULTS = {
products : [
{ value : 'Ash Bucket', data: 'AB', imagePath : 'img/ash_bucket/' },
{ value : 'Drill', data: 'DR', imagePath : 'img/drill/' },
{ value : 'Lights', data: 'LT', imagePath : 'img/light/' },
{ value : 'Shelve', data: 'SV', imagePath : 'img/shelve/' },
{ value : 'Maple Wood', data: 'MW', imagePath : 'img/maple' }
]
}
DropdownAC.prototype.loadImages = function(key_text){
return key_text
}
DropdownAC.prototype.appendImages = function () {
var top_result = $('.autocomplete-suggestion:first').text()
var ac_images = dropdownAC.loadImages(top_result)
console.log(ac_images)
}
DropdownAC.prototype.showPictureBox = function(){
//===============================
// Create pictureBox
//===============================
//--------------HERE------------------//
console.dir( dropdownAC.appendImages() )
}
//==================================
// VENDOR:autocomplete.js
//==================================
$('#autocomplete').autocomplete({
lookup : DropdownAC.DEFAULTS.products,
onSelect : function (suggestion){
alert('What will i do with this?' + suggestion.value + ', ' + suggestion.data);
}
})
//==================================
// Extension-autocomplte tweak
//==================================
$('#autocomplete').keyup(function(){
var suggestions_visible = $('.autocomplete-suggestions').is(":visible")
var suggestions_childLength = $('.autocomplete-suggestions').children().length
if ( suggestions_visible && suggestions_childLength > 0 ){
dropdownAC.showPictureBox()
}
else {
$('.suggestions_wrapper').remove()
}
})
window.dropdownAC = new DropdownAC();
$(document)
.on('load', dropdownAC);
})(window.jQuery);
最佳答案
因为DropdownAC.prototype.appendImages = function () {
不返回任何内容!
尝试将您的appendImages函数更改为此:
DropdownAC.prototype.appendImages = function () {
return 'hello, i'm here!';
}