Pass Initialized Object Reference to Event

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

    Pass Initialized Object Reference to Event

    If I initializer my object as

    var obj = new SomeObject();

    then I have some method inside of the object as:

    function SomeObject(){
    .......
    this.showLink = function(){
    return "<a href='javascrip t:void(0)' onClick='obj.sh owMessage()'></
    a>"
    }
    this.showMessag e = function(){ alert("My Message is Here") }
    .......
    }

    Obviously, "onClick='obj.s howMessage()' " is a wrong refrence because
    we may call it obj2 or have multiple objects. How can I reference any
    function of an initialized object on click of a link?
    Thank you for suggestions.
  • Thomas 'PointedEars' Lahn

    #2
    Re: Pass Initialized Object Reference to Event

    vunet wrote:
    If I initializer my object as
    >
    var obj = new SomeObject();
    >
    then I have some method inside of the object as:
    >
    function SomeObject(){
    ......
    this.showLink = function(){
    return "<a href='javascrip t:void(0)' onClick='obj.sh owMessage()'></
    a>"
    }
    this.showMessag e = function(){ alert("My Message is Here") }
    ......
    }
    >
    Obviously, "onClick='obj.s howMessage()' " is a wrong refrence because
    we may call it obj2 or have multiple objects. How can I reference any
    function of an initialized object on click of a link?
    Don't use strings. And move all methods that don't require a closure to the
    prototype object.

    function isMethod(o, p)
    {
    return (o && /\s*(function|ob ject|unknown)\s */i.test(typeof o[p])
    && o[p]);
    }

    function SomeObject()
    {
    this.showLink = function() {
    if (isMethod(docum ent, "createElement" ))
    {
    var a = document.create Element("a");

    if (a)
    {
    a.href = "javascript:voi d(0)";

    var me = this;
    var f = function(e) {
    me.showMessage( );

    if (isMethod(e, "preventDefault ")) e.preventDefaul t();
    if (typeof e.returnValue != "undefined" ) e.returnValue = false;
    return false;
    };

    if (isMethod(a, "addEventListen er"))
    {
    a.addEventListe ner("click", f, false);
    }
    else
    {
    a.onclick = f;
    }
    }
    }

    return a;
    };
    }

    SomeObject.prot otype = {
    constructor: SomeObject,
    showMessage: function(){ alert("My Message is Here") }
    };

    That said, don't misuse `a' elements like this.
    Thank you for suggestions.
    You're welcome.


    PointedEars
    --
    var bugRiddenCrashP ronePieceOfJunk = (
    navigator.userA gent.indexOf('M SIE 5') != -1
    && navigator.userA gent.indexOf('M ac') != -1
    ) // Plone, register_functi on.js:16

    Comment

    Working...