www.webdeveloper.com
Results 1 to 2 of 2

Thread: Proper syntax for adding methods to objects

Hybrid View

  1. #1
    Join Date
    Jul 2009
    Posts
    1

    Proper syntax for adding methods to objects

    Hi everyone ,

    I am developing an application used for some registration of data at my workplace, and landed on an AJAX architecture using a series of C# developed services and a "client application" in javascript for user interaction.

    I like to work in an object oriented fashion, and have previously defined objects as such:

    Code:
    function MyObject()
    {
      this.myMethod = myMethod;
    
      function myMethod() 
      { /* code */ }
    }
    var obj = new MyObject()
    However, this time I tried to simplify my code by doing:

    Code:
    function MyObject()
    {
      this.myMethod = function myMethod() 
      { /* code */}
    }
    var obj = new MyObject()
    This works well, and browsers have no problems with this. However, Netbeans does not manage to derive code completion suggestions from this code. It simply does not recognize myMethod as methods inside the MyObject object.

    Is this indicative that it is not correct somehow to assign function pointers the way I do?

    It is not major issue, but it does make my code clean, IMHO.

    Best, Knut

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    No, that's absolutely fine, and preferable in my opinion. JavaScript treats functions like variables (very like LISP), which is why you can pass functions around like variables.

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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