SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    May 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why do people use colons in javascript function declarations.

    Hi. I've been looking at the table sorting script available at http://www.workingwith.me.uk/article..._table_sorting and have a few questions about the coding style.

    I would like to know what is the difference between


    Code:
    init : function() {
    		// do stuff	
    		
    	},
    and

    Code:
    function init () {
    		// do stuff	
    		
    		
    	}

    Is this somthing to do with labels. What are the advantages and disadvantages of the two approaches?

    I look forward to learning from your answers.

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's because {} is an object literal and those functions are being defined as properties of the object.

    Here's an example:
    Code:
    var myObject = {
      foo : 'bar',
      talk : function() {
       alert('hello world')
      }
    }
    myObject.talk();
    alert(myObject.foo);
    The advantage of using the object literal is that you can keep a bunch of variables and functions grouped together and keep them out of the global scope which helps avoid naming conflicts.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Member
    Join Date
    May 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks

  4. #4
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's called JSON.

  5. #5
    SitePoint Member
    Join Date
    May 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to bother you again, I've hit another problem what is the difference between

    bold.onclick = this.revealMore;

    and

    bold.onclick = this.revealMore();

  6. #6
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zulubob View Post
    Sorry to bother you again, I've hit another problem what is the difference between
    One assigns the function to a variable, one calls the function and assigns the return value to a variable. Try this:

    Code:
    function foo() {
      return "bar";
    }
    
    var alpha = foo();
    var beta = foo;
    
    alert(alpha);
    alert(beta);
    You'll see the returned string "bar" in the first alert box, and a string representation of the function object in the second alert box.

    The beauty of this, is that if you assign a funciton to a variable, you can treat that variable as a function. So following on from the above,

    Code:
    alert(beta());
    Which shows a message box with "bar" in it, because you've called the function by putting () after it.

    In fact, there is very little difference between these two code snippets:

    Code:
    var foo = function() {};
    Code:
    function foo() {}
    Douglas
    Hello World

  7. #7
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by iulian View Post
    It's called JSON.
    Not really, JSON object's can't contain functions. Also, you have to quote the object keys, like so:

    Code:
    {
        "name": "steve",
        "age" : 29
    }
    Douglas
    Hello World

  8. #8
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, DougBTX, you're right .

    In fact is very clear also from the first paragraph of the JSON definition:
    JSON (JavaScript Object Notation) is a lightweight data-interchange format. [...] It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999.

  9. #9
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by iulian View Post
    In fact is very clear also from the first paragraph of the JSON definition
    Why'd you post it then?? Meh, doesn't matter.
    Hello World

  10. #10
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was confusing JSON and literal objects. Now it's clear what's the difference between them.


Bookmarks

Posting Permissions

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