让我的 JavaScript/jQuery 更干净

发布于 2024-10-18 03:46:08 字数 3436 浏览 2 评论 0原文

我正在尝试提高我的 JavaScript/jQuery 的简洁程度,并且想知道是否有人有任何指示。

当我看到这个时,看起来不太干净......

if (window.jQuery) {
    (function ($) { 

        var podCaption = function ($scope, settings) { 

            this._settings = $.extend({  
                openHeight : 75,
                expandHeight : 120,
                shrinkHeight : 30,
                closeHeight : 15,
                timer : ''
            }, settings); 
            this._elements = { 
                scope : $scope,
                caption : $('.slider-information', $scope)
            };  

            this.init();
        };

        podCaption.prototype.init = function() {
            var _this = this; 

            $('.photo-more', _this._elements.caption).live('click', function() {  
                _this.expand(_this);
            });

            _this._elements.caption.mouseenter(function() {  
                _this.open(_this);
            }).mouseleave(function() {
                _this._settings.timer = setTimeout(function() { 
                    _this.shrink(_this);
                }, 1000);
            }); 
        };

        podCaption.prototype.changeImage = function(photoIndex, image) {
            var _this = this; 

            //Shrink out content 
            _this.close(_this, function() { 
                //Build content - NOTE i'm actually doing some template stuff here but I'm trying to make the code a little less verbose for the question at hand 
                _this._elements.caption.empty();
                _this._elements.caption.append('<div><div class="photo-description">..</div><div class="photo-more">...</div><div class="photo-info">...</div></div>'); 

                _this.open(_this, function() {   
                    _this._settings.timer = setTimeout(function() { 
                        _this.shrink(_this);
                    }, 4500);
                });
            }); 
        }; 

        podCaption.prototype.expand = function(_this, callback) {
            clearTimeout(_this._settings.timer);

            var caption = _this._elements.caption;
            $('.photo-more', caption).hide();
            $('.photo-info', caption).fadeIn(); 
            caption.animate({ height : _this._setting.expandHeight, opacity : 0.8 }, 500, callback);
        }

        podCaption.prototype.open = function(_this, callback) {
            clearTimeout(_this._settings.timer);

            _this._elements.caption.animate({ height : _this._setting.openHeight, opacity : 0.8 }, 500, callback);
        }

        podCaption.prototype.close = function(_this, callback) {
            clearTimeout(_this._settings.timer);

            var caption = _this._elements.caption;
            caption.children().fadeOut();
            caption.animate({ height : _this._setting.closeHeight, opacity : 0.2 }, 400, callback);
        }

        podCaption.prototype.shrink = function(_this, callback) {
            clearTimeout(_this._settings.timer);

            var caption = _this._elements.caption;
            $('.photo-info', caption).fadeOut(function() { $('.photo-more', caption).show(); });
            caption.animate({ height : _this._setting.shrinkHeight, opacity : 0.3 }, 400, callback);
        }

        $.fn.podCaption = function (options) {
            return new podCaption(this, options);
        };  
    })(jQuery);
}  

I'm trying to improve how clean my JavaScript/jQuery is and was wondering if anyone has any pointers.

When I look at this is just doesn't look clean...

if (window.jQuery) {
    (function ($) { 

        var podCaption = function ($scope, settings) { 

            this._settings = $.extend({  
                openHeight : 75,
                expandHeight : 120,
                shrinkHeight : 30,
                closeHeight : 15,
                timer : ''
            }, settings); 
            this._elements = { 
                scope : $scope,
                caption : $('.slider-information', $scope)
            };  

            this.init();
        };

        podCaption.prototype.init = function() {
            var _this = this; 

            $('.photo-more', _this._elements.caption).live('click', function() {  
                _this.expand(_this);
            });

            _this._elements.caption.mouseenter(function() {  
                _this.open(_this);
            }).mouseleave(function() {
                _this._settings.timer = setTimeout(function() { 
                    _this.shrink(_this);
                }, 1000);
            }); 
        };

        podCaption.prototype.changeImage = function(photoIndex, image) {
            var _this = this; 

            //Shrink out content 
            _this.close(_this, function() { 
                //Build content - NOTE i'm actually doing some template stuff here but I'm trying to make the code a little less verbose for the question at hand 
                _this._elements.caption.empty();
                _this._elements.caption.append('<div><div class="photo-description">..</div><div class="photo-more">...</div><div class="photo-info">...</div></div>'); 

                _this.open(_this, function() {   
                    _this._settings.timer = setTimeout(function() { 
                        _this.shrink(_this);
                    }, 4500);
                });
            }); 
        }; 

        podCaption.prototype.expand = function(_this, callback) {
            clearTimeout(_this._settings.timer);

            var caption = _this._elements.caption;
            $('.photo-more', caption).hide();
            $('.photo-info', caption).fadeIn(); 
            caption.animate({ height : _this._setting.expandHeight, opacity : 0.8 }, 500, callback);
        }

        podCaption.prototype.open = function(_this, callback) {
            clearTimeout(_this._settings.timer);

            _this._elements.caption.animate({ height : _this._setting.openHeight, opacity : 0.8 }, 500, callback);
        }

        podCaption.prototype.close = function(_this, callback) {
            clearTimeout(_this._settings.timer);

            var caption = _this._elements.caption;
            caption.children().fadeOut();
            caption.animate({ height : _this._setting.closeHeight, opacity : 0.2 }, 400, callback);
        }

        podCaption.prototype.shrink = function(_this, callback) {
            clearTimeout(_this._settings.timer);

            var caption = _this._elements.caption;
            $('.photo-info', caption).fadeOut(function() { $('.photo-more', caption).show(); });
            caption.animate({ height : _this._setting.shrinkHeight, opacity : 0.3 }, 400, callback);
        }

        $.fn.podCaption = function (options) {
            return new podCaption(this, options);
        };  
    })(jQuery);
}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把人绕傻吧 2024-10-25 03:46:08

而不是这个:

if (window.jQuery) {
    (function ($) {     
        // code     
    })(jQuery);
}

这个怎么样:

(function($) {
    if ( !$ ) return;
    // code
}(jQuery));

另外,这个:

_this._elements.caption.empty();
_this._elements.caption.append('<div>...'); 

可以链接:

_this._elements.caption.empty().append('<div>...'); 

或者只是这个:

_this._elements.caption.html('<div>...'); 

另外,您可以以对象文字形式定义原型:

podCaption.prototype = {
    expand: function(_this, callback) {
        // code
    },
    changeImage: function(photoIndex, image) {
        // code
    },
    ...
};

Instead of this:

if (window.jQuery) {
    (function ($) {     
        // code     
    })(jQuery);
}

how about this:

(function($) {
    if ( !$ ) return;
    // code
}(jQuery));

Also, this:

_this._elements.caption.empty();
_this._elements.caption.append('<div>...'); 

can be chained:

_this._elements.caption.empty().append('<div>...'); 

or just this:

_this._elements.caption.html('<div>...'); 

Also, you could define the prototype in object literal form:

podCaption.prototype = {
    expand: function(_this, callback) {
        // code
    },
    changeImage: function(photoIndex, image) {
        // code
    },
    ...
};
北渚 2024-10-25 03:46:08
window.jQuery && (function($){
    var podCaption = function ($scope, settings) {

        this._settings = $.extend({
            openHeight : 75,
            expandHeight : 120,
            shrinkHeight : 30,
            closeHeight : 15,
            timer : ''
        }, settings);
        this._elements = {
            scope : $scope,
            caption : $('.slider-information', $scope)
        };

        this.init();
    };
    $.extend(podCaption.prototype,{
        init:function() {
            var that = this;

            $('.photo-more', this._elements.caption).live('click', function() {
                that.expand();
            });

            this._elements.caption.mouseenter(function() {
                that.open();
            }).mouseleave(function() {
                that._settings.timer = setTimeout(function() {
                    that.shrink();
                }, 1000);
            });
        },
        changeImage:function(photoIndex, image) {
            var that = this;

            //Shrink out content
            this.close(function() {
                //Build content - NOTE i'm actually doing some template stuff here but I'm trying to make the code a little less verbose for the question at hand
                that._elements.caption.empty();
                that._elements.caption.append('<div><div class="photo-description">..</div><div class="photo-more">...</div><div class="photo-info">...</div></div>');

                that.open(function() {
                    that._settings.timer = setTimeout(function() {
                        that.shrink();
                    }, 4500);
                });
            });
        },
        expand:function(callback) {
            clearTimeout(this._settings.timer);

            var caption = this._elements.caption;
            $('.photo-more', caption).hide();
            $('.photo-info', caption).fadeIn();
            caption.animate({
                height : this._setting.expandHeight,
                opacity : 0.8
            }, 500, callback);
        },
        open:function(callback) {
            clearTimeout(this._settings.timer);

            this._elements.caption.animate({
                height : this._setting.openHeight,
                opacity : 0.8
            }, 500, callback);
        },

        close:function(callback) {
            clearTimeout(this._settings.timer);

            var caption = this._elements.caption;
            caption.children().fadeOut();
            caption.animate({
                height : this._setting.closeHeight,
                opacity : 0.2
            }, 400, callback);
        },

        shrink:function(callback) {
            clearTimeout(this._settings.timer);

            var caption = this._elements.caption;
            $('.photo-info', caption).fadeOut(function() {
                $('.photo-more', caption).show();
            });
            caption.animate({
                height : this._setting.shrinkHeight,
                opacity : 0.3
            }, 400, callback);
        }
    });

    $.fn.podCaption = function (options) {
        return new podCaption(this, options);
    };
})(window.jQuery);

也许是这样的。主要变化:扩展原型,this范围,out if语句

window.jQuery && (function($){
    var podCaption = function ($scope, settings) {

        this._settings = $.extend({
            openHeight : 75,
            expandHeight : 120,
            shrinkHeight : 30,
            closeHeight : 15,
            timer : ''
        }, settings);
        this._elements = {
            scope : $scope,
            caption : $('.slider-information', $scope)
        };

        this.init();
    };
    $.extend(podCaption.prototype,{
        init:function() {
            var that = this;

            $('.photo-more', this._elements.caption).live('click', function() {
                that.expand();
            });

            this._elements.caption.mouseenter(function() {
                that.open();
            }).mouseleave(function() {
                that._settings.timer = setTimeout(function() {
                    that.shrink();
                }, 1000);
            });
        },
        changeImage:function(photoIndex, image) {
            var that = this;

            //Shrink out content
            this.close(function() {
                //Build content - NOTE i'm actually doing some template stuff here but I'm trying to make the code a little less verbose for the question at hand
                that._elements.caption.empty();
                that._elements.caption.append('<div><div class="photo-description">..</div><div class="photo-more">...</div><div class="photo-info">...</div></div>');

                that.open(function() {
                    that._settings.timer = setTimeout(function() {
                        that.shrink();
                    }, 4500);
                });
            });
        },
        expand:function(callback) {
            clearTimeout(this._settings.timer);

            var caption = this._elements.caption;
            $('.photo-more', caption).hide();
            $('.photo-info', caption).fadeIn();
            caption.animate({
                height : this._setting.expandHeight,
                opacity : 0.8
            }, 500, callback);
        },
        open:function(callback) {
            clearTimeout(this._settings.timer);

            this._elements.caption.animate({
                height : this._setting.openHeight,
                opacity : 0.8
            }, 500, callback);
        },

        close:function(callback) {
            clearTimeout(this._settings.timer);

            var caption = this._elements.caption;
            caption.children().fadeOut();
            caption.animate({
                height : this._setting.closeHeight,
                opacity : 0.2
            }, 400, callback);
        },

        shrink:function(callback) {
            clearTimeout(this._settings.timer);

            var caption = this._elements.caption;
            $('.photo-info', caption).fadeOut(function() {
                $('.photo-more', caption).show();
            });
            caption.animate({
                height : this._setting.shrinkHeight,
                opacity : 0.3
            }, 400, callback);
        }
    });

    $.fn.podCaption = function (options) {
        return new podCaption(this, options);
    };
})(window.jQuery);

maybe like this. main change:extend prototype,this scope,out if statement

用心笑 2024-10-25 03:46:08

尝试使用 JSLint http://www.jslint.com/ 来美化代码(如果您就是这样的话)。 。

Try using JSLint http://www.jslint.com/ to prettify the code if that's what you what...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文