Object creation on page load, default function and other public functions

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

    Object creation on page load, default function and other public functions

    Hi all,



    I have created an object with a default function; the default function
    exposes a public function as well.



    myobject = function(){
    var objects = [];
    function addLoad(func){v ar oldonload = window.onload;i f(typeof
    window.onload != 'function'){win dow.onload = func;}else{wind ow.onload =
    function(){if(o ldonload){oldon load();}func(); }}}
    function object(div){
    /* some code here */
    }
    return function(div){
    addLoad(functio n(){objects.pus h(new object(div))});
    myobject.test = function(txt){a lert(txt);};
    }
    }();
    object("mytextb ox");





    How would I reference the object itself in such a way that I do not have to
    use the object name?



    Meaning; I'd like to get rid of:

    myobject.test = function(txt){a lert(txt);};


    And have it read something like:

    this.test = function(txt){a lert(txt);};
    (but that will add the function to the window object which is something I do
    not want)

    Or

    var that = this;

    that.test = function(txt){a lert(txt);};

    (but that doesn't work also...)




    My guess is you can't because the object is being created as the page loads
    and there is no way to get its name until it's finished creating. (I don't
    like to add the function(s) with prototype because the function should
    operate as a singleton anyway and it... well... just looks wrong I guess)



    Regards


  • David Mark

    #2
    Re: Object creation on page load, default function and other publicfunctions

    On Dec 11, 5:14 pm, "Calm_Pear" <sorry...@dirty mail.comwrote:
    Hi all,
    >
    I have created an object with a default function; the default function
    That statement is a bit confused. You have indeed created an object
    (a function object.) It has no "default function", but does have two
    inner functions, one of which will apparently be used as a
    constructor. I suspect you want to use the outer "myobject" function
    as a constructor, but it is hard to see why.
    exposes a public function as well.
    >
    myobject = function(){
    var objects = [];
    function addLoad(func){v ar oldonload = window.onload;i f(typeof
    window.onload != 'function'){win dow.onload = func;}else{wind ow.onload =
    function(){if(o ldonload){oldon load();}func(); }}}
    function object(div){
    /* some code here */
    }
    return function(div){
    addLoad(functio n(){objects.pus h(new object(div))});
    myobject.test = function(txt){a lert(txt);};
    }}();
    >
    object("mytextb ox");
    >
    How would I reference the object itself in such a way that I do not have to
    use the object name?
    >
    Meaning; I'd like to get rid of:
    >
    myobject.test = function(txt){a lert(txt);};
    >
    And have it read something like:
    >
    this.test = function(txt){a lert(txt);};
    Call it as a constructor, but the results won't be particularly
    useful.

    myStrangeObject = new object("mytextb ox");

    BTW, constructors should have capitalized names. Unfortunately,
    "Object" is taken.
    (but that will add the function to the window object which is something I do
    not want)
    >
    Or
    >
    var that = this;
    >
    that.test = function(txt){a lert(txt);};
    >
    (but that doesn't work also...)
    The result is the same as with your previous attempt (a "test"
    property is added to the global object.)
    >
    My guess is you can't because the object is being created as the page loads
    Your theory has no basis in fact.
    and there is no way to get its name until it's finished creating. (I don't
    like to add the function(s) with prototype because the function should
    operate as a singleton anyway and it... well... just looks wrong I guess)
    You need to stop guessing. You should learn how objects work in
    JavaScript before trying to create them. This example seems too
    complex for a first attempt.

    Comment

    • Marc

      #3
      Re: Object creation on page load, default function and other public functions

      >>
      >I have created an object with a default function; the default function
      >
      That statement is a bit confused. You have indeed created an object
      (a function object.) It has no "default function", but does have two
      inner functions, one of which will apparently be used as a
      constructor. I suspect you want to use the outer "myobject" function
      as a constructor, but it is hard to see why.
      I'm sorry if my names are incorrect and caused confusion.

      I want to attach an event to 1 or more text boxes and onkeyup update a div,
      I stripped a lot of functionality in an attempt to focus on the issue.
      I create an object for every text box to keep track of several things
      including the div (and if the div doesn't exist create it).

      So there are two private functions (The 'object' constructor [with the wrong
      name 'object'] and the 'addLoad'), 1 public anonymous function that runs as
      soon as myobject is being called and as this happens exposes 1 new public
      function 'test'.

      In my original code the private function 'object' is called 'o' by the
      way...
      >myobject = function(){
      > var objects = [];
      > function addLoad(func){v ar oldonload = window.onload;i f(typeof
      >window.onloa d != 'function'){win dow.onload = func;}else{wind ow.onload =
      >function(){if( oldonload){oldo nload();}func() ;}}}
      > function object(div){
      > /* some code here */
      > }
      > return function(div){
      > addLoad(functio n(){objects.pus h(new object(div))});
      > myobject.test = function(txt){a lert(txt);};
      > }}();
      >>
      >object("mytext box");
      >>
      >How would I reference the object itself in such a way that I do not have
      >to
      >use the object name?
      >
      Call it as a constructor, but the results won't be particularly
      useful.
      >
      myStrangeObject = new object("mytextb ox");
      Sorry again, that should have been:
      myobject("textb ox");

      >My guess is you can't because the object is being created as the page
      >loads
      Your theory has no basis in fact.
      Could you please explain that?
      >
      >and there is no way to get its name until it's finished creating. (I
      >don't
      >like to add the function(s) with prototype because the function should
      >operate as a singleton anyway and it... well... just looks wrong I guess)
      >
      You need to stop guessing. You should learn how objects work in
      JavaScript before trying to create them. This example seems too
      complex for a first attempt.
      I can assure you this is not a first attempt and I have been reading up on
      this subject. But I can understand you got that feeling because I'm not
      particularly good when it comes to using the proper name for objects or
      function objects. (Please also note that English is not my native language)


      Comment

      Working...