admin管理员组

文章数量:1433480

I have the following code:

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();
                            }
                        }
                    });

What I would like to do is have a different set of buttons depending on the type of modal window that is being created. I tried to do this using the following code but I get an error:

                    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
                    });

The error that I get is on the line:

var btns1 = new {

Error message is:

Object doesn't support this action

I guess there is something wrong with the way I make the assignment but I am not sure how to do this. I hope someone can help me out.

Can someone help me by telling me what I am doing wrong.

I have the following code:

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();
                            }
                        }
                    });

What I would like to do is have a different set of buttons depending on the type of modal window that is being created. I tried to do this using the following code but I get an error:

                    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
                    });

The error that I get is on the line:

var btns1 = new {

Error message is:

Object doesn't support this action

I guess there is something wrong with the way I make the assignment but I am not sure how to do this. I hope someone can help me out.

Can someone help me by telling me what I am doing wrong.

Share Improve this question asked May 31, 2012 at 14:02 Alan2Alan2 24.7k86 gold badges276 silver badges481 bronze badges 2
  • 2 Ommit the new keyword. – asawyer Commented May 31, 2012 at 14:03
  • 2 You need a constructor(!) function(!!!) after the keyword "new". And don't use the word "class" in the context of Javascript - unless you really know how objects work in Javascript. It's NOT what you probably learned in school (which is "classical class-based inheritance") :-) - that's because there are no classes in JS, but you CAN use the word because you can simulate them, in which case using that word is okay. Instantiating an object, what you do, is not the same as creating an instance from a prototype (constructor function), which is why I don't just say "omit new". – Mörre Commented May 31, 2012 at 14:04
Add a ment  | 

2 Answers 2

Reset to default 5

omit the new for objects.

var btns1 = new { .. };

should be

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

alternativ way with new:

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

No need for the new operator: you can instantiate your new Object via the Object literal:

var btns1 = { ... };

本文标签: jqueryAnonymous classes in my javascriptStack Overflow