#286 enhancement
ronin-28987 (at lighthouseapp)

Construct a class with arbitrary number of arguments

Reported by ronin-28987 (at lighthouseapp) | August 16th, 2008 @ 04:45 PM | in 1.7

Adding the next piece of code to Class.Methods will make it possible to construct a class with an arbitrary number of arguments.


Class.Methods = {
  addMethods: function(source) {
    // ...
  },

  construct: function(args) {
    var init = this.prototype.initialize; // store original initialize
    this.prototype.initialize = Prototype.emptyFunction; // temporary empty initialize
    var instance = new this(); // create instance
    instance.initialize = this.prototype.initialize = init; // restore initialize
    instance.initialize.apply(instance, $A(args) ); // apply initialize
    return instance; // return new object
  }
};
// ------
// now we can use:
var someInstance = SomeClass.construct(["arg1", "arg2"]);

This can be very useful when you have "factory"-methods in which you don't need to know the number of arguments, as in:


function createClass(className) {
  var args = $A(arguments); args.shift();
  return window[className].construct(args);
}

Comments and changes to this ticket

  • Juriy Zaytsev

    Juriy Zaytsev August 16th, 2008 @ 07:04 PM

    • State changed from “new” to “enhancement”
    • Assigned user set to “Juriy Zaytsev”
    • Milestone set to 1.7

    That's clever.

    The only thing I'm not sure about is assigning of initialize property directly to an instance. This seems unnecessary and somewhat obtrusive. When "restoring" a prototype.initialize from init, why not call init directly via apply (and not assign it to an instance, but leave it on prototype as it was before)?

    
    Class.Methods.construct = function(args) {
      var init = this.prototype.initialize;
      this.prototype.initialize = Prototype.emptyFunction;
      var instance = new this();
      this.prototype.initialize = init;
      init.apply(instance, $A(arguments.length == 1 ? args : arguments));
      return instance;
    };
    

    This also allows to optionally pass arguments the usual way, rather than explicitly passing an array:

    
    Person.construct('Ivan', 'Petrovich');
    // or
    Person.construct(['Jan', 'Kowalski']);
    
  • ronin-28987 (at lighthouseapp)

    ronin-28987 (at lighthouseapp) August 16th, 2008 @ 07:43 PM

    I agree with you on applying initialize on the instance rather than restoring it. I don't agree on the arguments length check, because it will also split the first argument when it's meant to be an array

    
    var someClass = Class.create({
      initialize: function(someArray) {
        this.someArray = someArray;
      }
    });
    var someInstance = someClass.construct(["foo", "bar"]);
    
    someInstance.someArray
    // --> foo
    // --> expected ["foo", "bar"]
    
  • Juriy Zaytsev
  • ronin-28987 (at lighthouseapp)

    ronin-28987 (at lighthouseapp) August 18th, 2008 @ 11:17 AM

    
    Class.Methods.construct = function(args) {
      var init = this.prototype.constructor;
      this.prototype.constructor = Prototype.emptyFunction;
      var instance = new this();
      this.prototype.constructor = init;
      init.apply(instance, $A(args) );
      return instance;
    };
    

    is actually more correct I think and avoids an extra call to a function

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 01:55 AM

    • Tag changed from class, enhancement, function, needs_tests to function, needs_tests, section:lang

    [not-tagged:"class" tagged:"section:lang" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:07 AM

    • Tag changed from function, needs_tests, section:lang to needs_tests, section:lang

    [not-tagged:"function" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:26 AM

    • Tag changed from needs_tests, section:lang to missing:tests, section:lang

    [not-tagged:"needs_tests" tagged:"missing:tests" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:36 AM

    • Tag changed from missing:tests, section:lang to needs:tests, section:lang

    [not-tagged:"missing:tests" tagged:"needs:tests" bulk edit command]

  • T.J. Crowder

    T.J. Crowder November 16th, 2009 @ 04:50 PM

    • Assigned user cleared.

    [responsible:none bulk edit command]

  • Leigh George

    Leigh George May 14th, 2018 @ 05:40 PM

    You have got such a down to earth and talented writers. One of your australiawritings me make my resume when apparently it was not an easy task. But your writer performed it brilliantly.

  • john93

    john93 July 3rd, 2018 @ 07:44 AM

    If you start doing coding then you can join him because he provides us with good coding programs. The paper writing helper is one of them who did coding first them move to write. You can pick this syntax too.

  • shrey

    shrey July 3rd, 2018 @ 12:50 PM

    Pada permainan Poker Online, ada ber aneka macam meja jumlah pemain yang tersedia. Ada meja yang hanya untuk 3 pemain , 4 pemain dan bahkan sampai 9 pemain. Disini anda bisa pilih sesuai dengan keinginan anda apakah ingin bermain rame – rame atau hanya untuk sedikit pemain saja. Dan sudah banyak sekali orang yang mengemari Poker Online tersebut.
    DOMINOQQ ONLINE
    BandarQ Online

  • lifetime

    lifetime August 2nd, 2018 @ 10:57 AM

    This is also a very good post which I really enjoy reading. It is not everyday that I have the possibility to see something like this. Remedies for Alzheimer’s Disease

  • lifetime

    lifetime August 2nd, 2018 @ 11:30 AM

    Good to become visiting your weblog again, it has been months for me. Nicely this article that i've been waited for so long. I will need this post to total my assignment in the college, and it has exact same topic together with your write-up. Thanks, good share. Turmeric Tea

  • lifetime

    lifetime August 2nd, 2018 @ 11:31 AM

    wow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated. Remedies for High Blood Pressure

  • lifetime

    lifetime August 2nd, 2018 @ 11:31 AM

    You re in point of fact a just right webmaster. The website loading speed is amazing. It kind of feels that you're doing any distinctive trick. Moreover, The contents are masterpiece. you have done a fantastic activity on this subject! Remedies for Diarrhea

  • lifetime

    lifetime August 2nd, 2018 @ 11:31 AM

    This was among the best posts and episode from your team it let me learn many new things. Peppermint Oil

  • lifetime

    lifetime August 2nd, 2018 @ 11:32 AM

    Really appreciate this wonderful post that you have provided for us.Great site and a great topic as well i really get amazed to read this. Its really good. Cinnamon Tea

  • lifetime

    lifetime August 2nd, 2018 @ 11:32 AM

    I really thank you for the valuable info on this great subject and look forward to more great posts Ginger

  • lifetime

    lifetime August 2nd, 2018 @ 11:32 AM

    This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck How To Get Rid of Acne

  • lifetime

    lifetime August 5th, 2018 @ 10:22 AM

    I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts. lose weight

  • lifetime

    lifetime August 9th, 2018 @ 04:22 PM

    I quite like reading an article that can make people think. Also, thanks for allowing for me to comment! fun88

  • lifetime
  • lifetime

    lifetime August 15th, 2018 @ 10:23 AM

    I don t have the time at the moment to fully read your site but I have bookmarked it and also add your RSS feeds. I will be back in a day or two. thanks for a great site. Tech Exclusive

  • lifetime

    lifetime August 15th, 2018 @ 11:27 AM

    I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it. 321 How To

  • lifetime

    lifetime August 15th, 2018 @ 07:24 PM

    I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job ! Net Pets

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

People watching this ticket

Pages