最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - Anonymous classes in my javascript - Stack Overflow

matteradmin4PV0评论

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 = { ... };
Post a comment

comment list (0)

  1. No comments so far