我很困惑在哪里声明这些变量,所以我可以将它们用作obj.slider._minVal
& obj.slider._maxVal
。我收到JShint错误:
var _minVal = 75; ^期待':'而是看到' _minVal'。 var _minVal = 75; ^预期标识符,而是看到' ='。
var obj = obj || {};
obj.slider = {
var _minVal=75, //Confused where to define these variables so it's available
_maxVal=300;//in _createSlider();
init:function(){
console.log('Initialization of Slider');
var $this = $(this);
this._createSlider();
return false;
},
// setSliderMin:function(sliderId, minVal){
// },
_createSlider:function(){
$('.slider-range').slider({
range: true,
min: obj.slider._minVal,
max: obj.slider._maxVal,
values: [obj.slider._minVal, obj.slider._maxVal],
slide: function( event, ui ) {
$( '#amount' ).val( '$' + ui.values[ 0 ] + ' - $' + ui.values[ 1 ] );
var handle = $(this).find('.ui-slider-handle');
$('.ui-slider-handle:first').html('<div class="tooltip bottom slider-tip"><div class="tooltip-arrow"></div><div class="tooltip-inner">' + ui.values[0] + '</div></div>');
$('.ui-slider-handle:last').html('<div class="tooltip bottom slider-tip"><div class="tooltip-arrow"></div><div class="tooltip-inner">' + ui.values[1] + '</div></div>');
}
});
var value = $( '#slider-range' ).slider( 'option', 'values' );
$('.ui-slider-handle:first').html('<div class="tooltip bottom slider-tip"><div class="tooltip-arrow"></div><div class="tooltip-inner">' + value[0] + '</div></div>');
$('.ui-slider-handle:last').html('<div class="tooltip bottom slider-tip"><div class="tooltip-arrow"></div><div class="tooltip-inner">' + value[1] + '</div></div>');
}
};
obj.slider.init();
});
答案 0 :(得分:2)
成员变量的语法与成员函数的语法相同:
key1: val,
key2: val
更改对象以使用键/值语法:
obj.slider = {
_minVal: 75,
_maxVal: 300,
_createSlider: function() {...},
init: function() {...}
};
或者,如果您想要隐藏变量以及更自然的代码,您可能想要使用函数:
obj.slider = function() {
var _minVal = 75,
_maxVal = 300,
_createSlider = function() {...};
(this.init = function() {...})();
};
var slider = new obj.slider();
// later on you can reset by calling
slider.init();
答案 1 :(得分:1)
使用:
创建一个属性,然后将其与this
一起使用: -
var obj = obj || {};
obj.slider = {
_minVal: 75,
_maxVal: 300,
init: function() {
console.log('Initialization of Slider');
var $this = $(this);
this._createSlider();
return false;
},
_createSlider: function() {
$('.slider-range').slider({
range: true,
min: this._minVal,
max: this._maxVal,
values: [this._minVal, this._maxVal],
slide: function(event, ui) {
$('#amount').val('$' + ui.values[0] + ' - $' + ui.values[1]);
var handle = $(this).find('.ui-slider-handle');
$('.ui-slider-handle:first').html('<div class="tooltip bottom slider-tip"><div class="tooltip-arrow"></div><div class="tooltip-inner">' + ui.values[0] + '</div></div>');
$('.ui-slider-handle:last').html('<div class="tooltip bottom slider-tip"><div class="tooltip-arrow"></div><div class="tooltip-inner">' + ui.values[1] + '</div></div>');
}
});
var value = $('#slider-range').slider('option', 'values');
$('.ui-slider-handle:first').html('<div class="tooltip bottom slider-tip"><div class="tooltip-arrow"></div><div class="tooltip-inner">' + value[0] + '</div></div>');
$('.ui-slider-handle:last').html('<div class="tooltip bottom slider-tip"><div class="tooltip-arrow"></div><div class="tooltip-inner">' + value[1] + '</div></div>');
}
};
obj.slider.init();
答案 2 :(得分:1)
@BenG的答案很棒,但您也可以创建一个类而不是使用JSON对象文字来使您的变量/函数变为私有。
var obj = obj || {};
obj.slider = function(){
var _minVal = 75;
var _maxVal = 300;
console.log('Initialization of Slider');
var $this = $(this);
_createSlider();
return false;
function _createSlider() {
$('.slider-range').slider({
range: true,
min: _minVal,
max: _maxVal,
values: [_minVal, _maxVal],
slide: function(event, ui) {
$('#amount').val('$' + ui.values[0] + ' - $' + ui.values[1]);
var handle = $(this).find('.ui-slider-handle');
$('.ui-slider-handle:first').html('<div class="tooltip bottom slider-tip"><div class="tooltip-arrow"></div><div class="tooltip-inner">' + ui.values[0] + '</div></div>');
$('.ui-slider-handle:last').html('<div class="tooltip bottom slider-tip"><div class="tooltip-arrow"></div><div class="tooltip-inner">' + ui.values[1] + '</div></div>');
}
});
var value = $('#slider-range').slider('option', 'values');
$('.ui-slider-handle:first').html('<div class="tooltip bottom slider-tip"><div class="tooltip-arrow"></div><div class="tooltip-inner">' + value[0] + '</div></div>');
$('.ui-slider-handle:last').html('<div class="tooltip bottom slider-tip"><div class="tooltip-arrow"></div><div class="tooltip-inner">' + value[1] + '</div></div>');
}
};
new obj.slider();