首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery $.extend()未扩展对象

jQuery $.extend()未扩展对象
EN

Stack Overflow用户
提问于 2013-04-05 08:58:35
回答 2查看 7.3K关注 0票数 2

使用一个简单的日历脚本,尝试将事件发布到适当的日期;然而,jQuery $.extend并没有扩展我的options对象。以下是HTML文件中的代码片段:

代码语言:javascript
复制
$("#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"
    }]
});

和我的插件的开始:

代码语言:javascript
复制
(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...

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

EN

回答 2

Stack Overflow用户

发布于 2013-04-05 09:01:09

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

票数 3
EN

Stack Overflow用户

发布于 2013-04-05 09:04:39

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

另外:记住Target是第一个对象...因此,在本例中,缺省值将扩展为选项...

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15824240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档