我的javascript中的匿名类

时间:2012-05-31 14:02:37

标签: javascript jquery

我有以下代码:

var modal = $.modal({
                        title: title,
                        closeButton: true,
                        content: content,
                        width: 1000,
                        maxHeight: 850,
                        resizeOnLoad: true,
                        buttons: {
                            'Submit': function (win) {
                                submitHandler($link, $('#main-form'));
                            },
                            'Submit & Close': function (win) {
                                var rc = submitHandler($link, $('#main-form'));
                                if (rc == true) { win.closeModal(); }
                            },
                            'Close': function (win) {
                                win.closeModal();
                            }
                        }
                    });

我想要做的是根据正在创建的模态窗口的类型设置不同的按钮组。我尝试使用以下代码执行此操作,但出现错误:

                    if (title.substr(0, 4) == "Crea") {
                        title += $('#RowKey').val();
                        var btns = btns1;
                    }
                    if (title.substr(0, 4) == "Edit") {
                        var btns = btns1;
                    }
                    if (title.substr(0, 4) == "Dele") {
                        var btns = btns2;
                    }
                    var btns1 = new {
                        'Submit': function (win) {
                            submitHandler($link, $('#main-form'));
                        },
                        'Submit & Close': function (win) {
                            var rc = submitHandler($link, $('#main-form'));
                            if (rc == true) { win.closeModal(); }
                        },
                        'Close': function (win) {
                            win.closeModal();
                        }
                    }
                    var btns2 = new {
                        'Submit & Close': function (win) {
                            var rc = submitHandler($link, $('#main-form'));
                            if (rc == true) { win.closeModal(); }
                        },
                        'Close': function (win) {
                            win.closeModal();
                        }
                    }
                    var modal = $.modal({
                        title: title,
                        closeButton: true,
                        content: content,
                        width: 1000,
                        maxHeight: 850,
                        resizeOnLoad: true,
                        buttons: btns
                    });

我得到的错误就行了:

var btns1 = new {

错误信息是:

Object doesn't support this action

我想我的作业方式有问题,但我不知道该怎么做。我希望有人可以帮助我。

有人可以告诉我我做错了什么来帮助我。

2 个答案:

答案 0 :(得分:5)

省略对象的new

var btns1 = new { .. };

应该是

var btns1 = {someProperty: someValue, ... };

替代新方式:

var bts1 = new Object();
btns1.someProperty = someValue; ...

答案 1 :(得分:1)

不需要new运算符:您可以通过Object文字实例化新对象:

var btns1 = { ... };