having more than one object without using prototype

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Black

    having more than one object without using prototype

    I am working on my own pop up calendar, mainly because the one I am
    currently using crashes the Safari browser at times.

    So, I want to verify that what I am doing will work, in that I want to
    be able to have multiple calendars open at the same time, each
    independent of the other.

    So, I start it off with:
    var Calendar = {
    dateSelected: null,
    topPos:null,
    leftPos:null,

    somefunction:fu nction(e) {
    ...
    }
    };

    If I create more than one calendar object, will they have their own
    variables, in that the dateSelected, topPos and leftPos will be unique
    to that instance?

    Or, is there a better way to do this, that is cross-platform.

    Thank you for your help.

  • VK

    #2
    Re: having more than one object without using prototype


    James Black wrote:
    var Calendar = {
    dateSelected: null,
    topPos:null,
    leftPos:null,
    >
    somefunction:fu nction(e) {
    ...
    }
    };
    >
    If I create more than one calendar object, will they have their own
    variables, in that the dateSelected, topPos and leftPos will be unique
    to that instance?
    >
    Or, is there a better way to do this, that is cross-platform.
    function Calendar(arg) {
    this.name = arg;
    this.dataSelect ed = null;
    this.topPos = null;
    this.leftPos = null;
    }

    Calendar.protot ype.somefunctio n = function(e) {
    alert(this.name );
    }

    var cal1 = new Calendar('First ');
    var cal2 = new Calendar('Secon d');

    cal1.somefuncti on();
    cal2.somefuncti on();

    Comment

    • VK

      #3
      Re: having more than one object without using prototype


      VK wrote:
      function Calendar(arg) {
      this.name = arg;
      this.dataSelect ed = null;
      this.topPos = null;
      this.leftPos = null;
      }
      >
      Calendar.protot ype.somefunctio n = function(e) {
      alert(this.name );
      }
      >
      var cal1 = new Calendar('First ');
      var cal2 = new Calendar('Secon d');
      >
      cal1.somefuncti on();
      cal2.somefuncti on();
      Oops... : "having more than one object without using prototype"

      There are other ways, but what is the exact problem with prototype? I
      know that Safari 1.x-2.x is a junk, but still they did not fall so low
      do not support prototype property.

      Comment

      • Oliven

        #4
        Re: having more than one object without using prototype



        On 23 jan, 18:53, "James Black" <planiturth...@ gmail.comwrote:
        I am working on my own pop up calendar, mainly because the one I am
        currently using crashes the Safari browser at times.
        >
        So, I want to verify that what I am doing will work, in that I want to
        be able to have multiple calendars open at the same time, each
        independent of the other.
        >
        So, I start it off with:
        var Calendar = {
        dateSelected: null,
        topPos:null,
        leftPos:null,
        >
        somefunction:fu nction(e) {
        ...
        }
        >
        };If I create more than one calendar object, will they have their own
        variables, in that the dateSelected, topPos and leftPos will be unique
        to that instance?
        >
        Or, is there a better way to do this, that is cross-platform.
        >
        Thank you for your help.
        function Calendar(arg) {
        this.name = arg;
        this.dataSelect ed = null;
        this.topPos = null;
        this.leftPos = null;
        this.somefuncti on = calendar_somefu nction;

        }

        function calendar_somefu nction(e) {
        alert(this.name );
        }

        var cal1 = new Calendar('First ');
        var cal2 = new Calendar('Secon d');

        cal1.somefuncti on();
        cal2.somefuncti on();

        Comment

        • James Black

          #5
          Re: having more than one object without using prototype


          VK wrote:
          >
          Oops... : "having more than one object without using prototype"
          >
          There are other ways, but what is the exact problem with prototype? I
          know that Safari 1.x-2.x is a junk, but still they did not fall so low
          do not support prototype property.
          Actually the problem is with prototype.js, or the prototype
          framework, not with javascript prototypes.

          Sorry, I was unclear in my first message.

          Comment

          • James Black

            #6
            Re: having more than one object without using prototype


            Oliven wrote:
            function Calendar(arg) {
            this.name = arg;
            this.dataSelect ed = null;
            this.topPos = null;
            this.leftPos = null;
            this.somefuncti on = calendar_somefu nction;
            >
            }
            >
            function calendar_somefu nction(e) {
            alert(this.name );
            }
            >
            var cal1 = new Calendar('First ');
            var cal2 = new Calendar('Secon d');
            >
            cal1.somefuncti on();
            cal2.somefuncti on();
            Thank you to both of your for your responses.

            I will probably not use the prototype way until I can feel more
            comfortable with that, as it is one aspect of javascript that I have
            limited experience with.

            Comment

            • James Black

              #7
              Re: having more than one object without using prototype

              I hope you two don't mind a question on how I would like to do this.

              I am using namespaces, and so, is there any reason why this approach
              would be bad?

              var AcompCalendar = {
              Calendar:functi on(arg) {
              this.name = arg;
              this.dateSelect ed = null;
              this.topPos = null;
              this.leftPos = null;
              this.nextMonth = AcompCalendar.n extMonth;
              };

              nextMonth:funct ion() {
              alert('next month selected');
              }
              }


              var cal1 = new AcompCalendar.C alendar('first one');
              var cal2 = new AcompCalendar.C alendar('second one');


              Thank you for your help.

              Comment

              • James Black

                #8
                Re: having more than one object without using prototype


                James Black wrote:
                I hope you two don't mind a question on how I would like to do this.
                >
                I am using namespaces, and so, is there any reason why this approach
                would be bad?
                >
                var AcompCalendar = {
                Calendar:functi on(arg) {
                this.name = arg;
                this.dateSelect ed = null;
                this.topPos = null;
                this.leftPos = null;
                this.nextMonth = AcompCalendar.n extMonth;
                },
                >
                nextMonth:funct ion() {
                alert('next month selected');
                }
                }
                >
                >
                var cal1 = new AcompCalendar.C alendar('first one');
                var cal2 = new AcompCalendar.C alendar('second one');
                >
                >
                Thank you for your help.
                I had a typo that I corrected here, I should have commas separating
                functions not semi-colons.

                Comment

                • RobG

                  #9
                  Re: having more than one object without using prototype



                  On Jan 24, 6:23 am, "James Black" <planiturth...@ gmail.comwrote:
                  I hope you two don't mind a question on how I would like to do this.
                  >
                  I am using namespaces, and so, is there any reason why this approach
                  would be bad?
                  >
                  var AcompCalendar = {
                  Calendar:functi on(arg) {
                  this.name = arg;
                  this.dateSelect ed = null;
                  this.topPos = null;
                  this.leftPos = null;
                  this.nextMonth = AcompCalendar.n extMonth;
                  };
                  >
                  nextMonth:funct ion() {
                  alert('next month selected');
                  }
                  >
                  }var cal1 = new AcompCalendar.C alendar('first one');
                  var cal2 = new AcompCalendar.C alendar('second one');
                  "Bad" is likely too strong a word. The primary design flaws are:

                  1. It replicates methods and properties in every instance of the
                  calendar object that could be implemented only once in the calendar
                  object's prototype, therefore it is wasteful of resources.

                  2. If you change the name of the namespace (i.e. AcompCalendar), you
                  have to find and replace all instances of that namespace.

                  3. The (otherwise) unnecessary use of references to access methods,
                  e.g.

                  var AcompCalendar = {
                  Calendar : function(arg) {
                  ...
                  this.nextMonth = AcompCalendar.n extMonth;
                  },
                  nextMonth : function() {
                  ...
                  }
                  }

                  Of course there are pros and cons with other approaches too, but you
                  only asked for the cons of this approach. :-)

                  There are many different ways to implement this, what is best for you
                  is your decision. A namespaced version using the calendar object's
                  prototype might look like:

                  var AcompCalendar = {}

                  AcompCalendar.c alendar = function(arg) {
                  this.name = arg;
                  this.dateSelect ed = null;
                  this.topPos = null;
                  this.leftPos = null;
                  }

                  AcompCalendar.c alendar.prototy pe.nextMonth = function() {
                  alert(this.name + ': next month selected');
                  }

                  var cal1 = new AcompCalendar.c alendar('first one');
                  var cal2 = new AcompCalendar.c alendar('second one');

                  cal1.nextMonth( )
                  cal2.nextMonth( )

                  You might also like to read Richard Cornford's article:

                  <URL: http://www.litotes.demon.co.uk/js_in...te_static.html >

                  Take your time, there's lots to learn but once learned, it's very
                  valuable.


                  --
                  Rob

                  Comment

                  Working...