javascript - js中属性是一个大括号,里面对应很多方法,他们之间的层次关系是什么,能帮解释一下吗
天蓬老师
天蓬老师 2017-04-10 16:53:16
[JavaScript讨论组]
var newPage = {};
newPage.bandwidth = {
        init : function(){
            var _this = this;
            //初始化有效区域
            _this.config.xMin = $('.bwTabCon').offset().left;
            _this.config.xMax = $('.bwTabCon').offset().left + $('.bwTabCon').width();
            _this.config.yMin = $('.bwTabCon').offset().top;
            _this.config.yMax = $('.bwTabCon').offset().top + $('.bwTabCon').height();
            //鼠标动作
            newPage.mouseSelect.init();
        },
        config : {
            xMin : null,
            xMax : null,
            yMin : null,
            yMax : null,
            currentId : 0
        },
        alertSureClick : function(){
            var selectedInput = $('.bwTabNav input:checked');
            this.delAlertp();
            this.Reset();
            return false;
        },
        Reset : function(){
            $('.bwTabNav input').each(function(index, element) {
                element.checked = false;
            });
            $('.time_select .select').removeClass('select');
        },
        timeSelect : function(t){
            this.showDarkp();
        },
        showDarkp : function(){
            var _opacity = 0.6 ; 
            var htmlOpacity = '

'; $('body').append(htmlOpacity); $('#opacity').css({'height':$(document).height()}).animate({opacity:_opacity}).fadeIn(200); }, delAlertp : function(){ $('.newPageAlertp,#opacity').remove(); $('#overp').hide() } }; newPage.mouseSelect = { init : function(){ var _this = this; $('.bwTabCon').mousedown(function(e){ $('#overp').show() $('#selectArea').remove(); var selectArea = '

' _this.config.startX = e.pageX; _this.config.start_Y = e.pageY; _this.config.down = true; _this.currentBox().append(selectArea); $('#selectArea').fadeTo(200, 0.5); }).mouseup(function(e){ _this.mouseUp(e); }); $('.bwTabCon').mousemove(function(e){ return _this.mouseOver(e); }); $('.bwTabCon').mousemove(function(e){ return _this.mouseOver(e); }).mouseout(function(e){ if(e.pageX > newPage.bandwidth.config.xMax || e.pageX < newPage.bandwidth.config.xMin || e.pageY < newPage.bandwidth.config.yMin || e.pageY > newPage.bandwidth.config.yMax){ _this.config.down = false; $('#selectArea').remove(); } }); }, config : { down : false, ihide : true, currentId : 0, currentTarget : null, startX : 0, startY : 0 }, checkAttr : function(obj, value, type){ var value = value || 'select'; var type = type || 'class'; if($(obj).attr(type) && $(obj).attr(type).indexOf(value) >= 0){ return true; }else{ return false; } }, mouseOver : function(event){ var width,left; width = Math.abs(event.pageX - this.config.startX) if(event.pageX - this.config.startX >=0){ left = this.config.startX - $('.bwTabCon').offset().left; }else{ left = this.config.startX - $('.bwTabCon').offset().left - width; } $('#selectArea').css({'left':left,'width' : width}).show(); }, addClassSelect : function(target){ $(target).addClass('select').attr('name','p'+this.config.currentId); }, removeClassSelect : function(target){ $(target).removeClass('select').attr('name',''); }, mouseUp : function(event){ this.config.startY = event.pageX; this.config.down = false; this.fewItemAdd(); this.showSetBox(); this.config.currentId++; $('#selectArea').remove(); }, showSetBox : function(){ if ( checkWeekSelect()==false ) { return; } if(this.config.ihide == false) {return ;} var bandwidth = newPage.bandwidth; bandwidth.showDarkp(); //if($('#alertp').css('display') != 'none'){return false;}; var box = $('#alertp').clone(true); box.addClass('newPageAlertp'); this.config.ihide = false; $('body').append(box); setTimeout(function(){ box.fadeIn(400); },400) this.setSelectedTime(); }, fewItemAdd : function(){ var _this = this,xMin,xMax; this.config.startX > this.config.startY ? xMin = this.config.startY : xMin = this.config.startX ; this.config.startX > this.config.startY ? xMax = this.config.startX : xMax = this.config.startY ; $('#bwTab_'+ newPage.bandwidth.config.currentId + ' .time_select p').each(function(index,item){ if(!_this.checkAttr(item,'p'+ _this.config.currentId,'name') && checkInclude(item)){ if(_this.checkAttr(item)){ _this.removeClassSelect(item); }else{ _this.addClassSelect(item); } } }) function checkInclude(i){ if(($(i).offset().left + $(i).width()) > xMin && $(i).offset().left < xMax){ return true; } } }, currentBox : function(){ return $('#bwTab_'+ newPage.bandwidth.config.currentId + ' .time_select'); }, setSelectedTime : function(){ var selectItems = $('.time_select p:[name = "p' + this.config.currentId + '"]'); last = $('.newPageAlertp #time_to option[value = "'+selectItems.last().html() +'"]'); if(selectItems.length>1){ myValue = $(selectItems[selectItems.length -1]).html(); myValueInt = parseInt(myValue, 10); if (myValueInt < 24) { myValueInt ++; } if (myValueInt < 10) { myValue = "0" + myValueInt; } else { myValue = "" + myValueInt; } last = $('.newPageAlertp #time_to option[value = "'+ myValue +'"]'); } $('.newPageAlertp #time_from option[value = "'+selectItems.first().html() +'"]')[0].selected = true $(last)[0].selected = true; } }
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(7)
PHPz
var newPage = {}; //等价于 var newPage = new Object(); 这种方式不建议使用

再比如:

var Person = {
    action: function(){
        return "doing";
    }
}

等价于

var Person = new Object();
Person.action = function () {
    return "doing";
}
怪我咯

大括号就是对象直接量啊,表示这一个属性就是一个对象,层次关系很明显的…

黄舟

我都是理解为一个大数组的,个人观点。

阿神
var a = {}

就是新建一个对象的语法糖吧

但是{}本身通常表示一个块级(block)

天蓬老师

对象里面的对象

``

迷茫

建议楼主学习一下JSON数据结构,数据格式

阿神

教你一个简单的方法:把所有函数用fn代替,然后会变成:

var newPage = {};

newPage.bandwidth = {
    init : fn,
    config : fn,
    alertSureClick : fn,
    Reset : fn,
    timeSelect : fn,
    showDarkp : fn,
    delAlertp : fn
};

newPage.mouseSelect = {
    init : fn,
    config : fn,
    checkAttr : fn,
    mouseOver : fn,
    addClassSelect : fn,
    removeClassSelect : fn,
    mouseUp : fn,
    showSetBox : fn,
    fewItemAdd : fn,
    currentBox : fn,
    setSelectedTime : fn
};

怎么样,是不是好理解多了。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号