www.webdeveloper.com
Results 1 to 8 of 8

Thread: private callback access to this

  1. #1
    Join Date
    Nov 2008
    Posts
    2

    Talking private callback access to this

    Someone please tell me if this looks legit.

    function Object()
    {
    this.publicMethod = publicMethod;
    this.publicMember = 'Hello World!';

    var that = this;

    function publicMethod(element)
    {
    element.onclick = 'privateMethod()';
    }

    function privateMethod()
    {
    alert(that.publicMember);
    }
    }

    I typed it up just now as an example but its a simplification of another working object. So ignore any syntax errors and just look at the concept. It works. Is it a good idea? Will I run into bugs later? Is it w3c kosher? I haven't found any documentation on this online.

  2. #2
    Join Date
    Oct 2008
    Posts
    349
    BTW, JavaScript doesn't support true private/public classing (at least not yet), what you are doing is using scope/closure to emulate it. There's not wrong with that though.

    In this case, you need to follow the scope/closure rules, you can't define onclick using a string'ed code, it would be like attempting to call "privateMethod()" from a global context, so you need to change it to
    Code:
    element.onclick = privateMethod;
    Regarding kosher-ness, JavaScript is governed by ECMA, not W3C. The HTML DOM interface is governed by W3C.

    I have actually tested your mocked code:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function jObject()
    {
    	this.publicMethod = publicMethod;
    	this.publicMember = 'Hello World!';
    
    	var that = this;
    
    	function publicMethod(element)
    	{
    		alert(element);
    		element.onclick = privateMethod;
    	}
    
    	function privateMethod()
    	{
    		alert(that.publicMember);
    	}
    }
    
    window.onload = function ()
    {
    var a = new jObject();
    a.publicMethod(document.getElementById('hi'));
    }
    </script>
    
    </head>
    <body>
    <a id="hi">Click here!</a>
    </body>
    </html>
    Last edited by voidvector; 11-19-2008 at 12:56 AM.

  3. #3
    Join Date
    Nov 2008
    Posts
    2

    My Bad

    Yeah, I intended to write it as:

    Code:
    element.onclick = privateMethod;
    My real concern is over:

    Code:
    var that = this;
    I started in C++ so I tend to think about how objects exist memory. I assume that because the functions and vars are in the scope of the greater object, that they are created with the object. So if you were to create a dozen of these objects, you would have a dozen copies of each function and var. So far this appears to be true in FireFox.

    My question is really about how this works in terms of cross browser compatibility, especially when the situation is complicated by a callback. When I assign a callback, will it reference that specific instance of the function? If so, will the function always have access to everything that was defined in it's scope?

  4. #4
    Join Date
    Feb 2008
    Posts
    1,666
    Quote Originally Posted by BrickHouse View Post
    Someone please tell me if this looks legit.

    function Object()
    That is not legit because JavaScript already has an Object() function/class/constructor.

  5. #5
    Join Date
    Oct 2008
    Posts
    349
    Cross-browser should be fine. I had project with dynamically defined functions passed around like candy, and the internal code actually worked flawlessly on IE4. On Gecko side it work on everything from Netscape 6 and up.

    The project as a whole only worked on IE 5 due to the lack of W3C DOM support on IE4.

  6. #6
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,774
    Or maybe try this:
    HTML Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function bindFunction(scope, args, fn) {
      if (!args) {
        args = [];
      }
      return function() { return fn.apply(scope,args) };
    }
    
    function jObject()
    {
      this.publicMember = 'Hello World!'
    }
    jObject.prototype = {
      
      publicMember : '',
      
      publicMethod : function(element)
      {
        function privateMethod(foo) {
          alert( this.publicMember );
          alert( foo );
        }
        element.onclick = bindFunction(this, ["bar"], privateMethod);
      }
      
    };
    
    window.onload = function ()
    {
      var a = new jObject();
      a.publicMethod(document.getElementById('hi'));
    }
    </script>
    
    </head>
    <body>
    <a id="hi">Click here!</a>
    </body>
    </html>

  7. #7
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    BrickHouse, the main problem with this kind of code organization is performance. New copies of every function are created with every new object instance. That means when there are many instances of real and large classes organized this way (I say "class" loosely, of course), then the code will consume a lot of memory and run slowly.

    So while this technically works, it isn't always practical. As it turns out, there's no good way to do this. Eventually we need to accept that JavaScript doesn't support private members, and the best we can do is mark private members with an underscore.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  8. #8
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,774
    I second Jeff Mott's opinion. For too long we have tried hacking JavaScript to be an OOP language like C++, and all we do is create more work for the browser. JavaScript is what it is: A language with prototype based classes in which all class members are public.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles