jQuery $ .extend()没有扩展对象

时间:2013-04-05 00:58:35

标签: jquery object

使用简单的日历脚本,尝试将事件发布到正确的日期;但是jQuery $.extend没有扩展我的options对象。以下是HTML文件的摘录:

$("#cal").calendar({
    month: 2,
    events: [{
        date: "2013-4-10",
        workorder: 1234567890,
        district: 01,
        range: "2:00pm - 4:00pm",
        appNotes: "This is just a sample of some notes... end",
        installer: "John Doe",
        cityNotes: "This is just another sample of some notes... end"
    }, {
        date: "2013-4-1",
        workorder: 1234567890,
        district: 01,
        range: "3:00pm - 5:00pm",
        appNotes: "This is just a sample of some notes... end",
        installer: "John Doe",
        cityNotes: "This is just another sample of some notes... end"
    }]
});

插件的开头:

(function($){
    $.fn.calendar = function(options){
        var defaults= {
            month: null,
            prev: "prevMonth",
            curr: "currMonth",
            next: "nextMonth",
            events: []
        }, options = $.extend(defaults, options);
        return($(this).each(function(){
            var obj = $(this),
            // Etc...

我能够访问该对象的属性,但是没有像想象的那样进行扩展。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

jQuery.extend扩展了传入的第一个对象 - 而不是$.extend(defaults, options)$.extend({}, defaults, options)

答案 1 :(得分:1)

(function($){
    $.fn.calendar = function(options){
        var defaults= {
            month: null,
            prev: "prevMonth",
            curr: "currMonth",
            next: "nextMonth",
            events: []
        };
        options = $.extend(defaults, options);

另外:记住目标是第一个对象......所以在这个例子中,默认值会用选项扩展...