gpt4 book ai didi

javascript - 期望 ':' 而不是看到 '_variableName'

转载 作者:行者123 更新时间:2023-11-30 09:48:20 25 4
gpt4 key购买 nike

我很困惑在哪里声明这些变量,所以我可以将它们用作 obj.slider._minValobj.slider._maxVal。我收到 JShint 错误:

var _minVal = 75; ^ Expected ':' and instead saw '_minVal'. var _minVal = 75; ^ Expected an identifier and instead saw '='.

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();

});

最佳答案

成员变量的语法与成员函数的语法相同:

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();

JSFiddle

关于javascript - 期望 ':' 而不是看到 '_variableName',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37798295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com