This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes A bit of confusion regarding the function in javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "A bit of confusion regarding the function in javascript" Watch "A bit of confusion regarding the function in javascript" New topic
Author

A bit of confusion regarding the function in javascript

Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Hi I was a bit curious about the function object in javascript. As to why the following is possible



Is there a concept i am missing ?? I would really like it if someone clarifies this up. Since i thought adding properties as above was only possible with maps

var m={};
m["a"]=value 1;

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Functions in JavaScript are first-class objects. They have all the behaviors of other objects in JavaScript. Like other objects, functions can:
  • Be assigned to variables and properties.
  • Be created via literals.
  • Be passed as function arguments.
  • Be returned as function values.
  • and, the one that answers your question
  • Be assigned properties.


  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61310
        
      66

    Rajesh Khan wrote:i thought adding properties as above was only possible with maps

    There's not really an such thing as a "map" in JavaScript, per se. All objects in JavaScript are a collection of name-value pairs. So any object type can accept properties. And since a function is just another object, it can also accept properties.

    So in that sense, all JavaScript objects are "maps".

    You can even do something like:
    because strings are just objects too. (What good this will do you is questionable, but it's possible.)
    Rajesh Khan
    Ranch Hand

    Joined: Oct 16, 2011
    Posts: 230
    Oh okay thanks for clarifying that out. It really explains a lot. However one last thing is a variable considered an object . I dont think it is since The code below doesn not allow it to assign a prooperty


    I guess the following list displays all the objects http://www.devx.com/projectcool/Article/19993
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61310
        
      66

    var a;
    a.x ="Assign a property";

    When you declare a variable it initially has the value undefined. If you want it to reference an empty Object, the code should be:
    Rajesh Khan
    Ranch Hand

    Joined: Oct 16, 2011
    Posts: 230
    Bear Bibeault wrote:
    var a;
    a.x ="Assign a property";

    When you declare a variable it initially has the value undefined. If you want it to reference an empty Object, the code should be:


    Oh okay now that makes more sense.. since there is nothing like a map per say in javascript as all objects are name value pairs.
    I tried doing this to proove a point and it works
    var a=[]
    a.ab="Someproperty";

    Thanks again , for clearing that up..
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61310
        
      66

    Rajesh Khan wrote:
    I tried doing this to proove a point and it works
    var a=[]
    a.ab="Someproperty";

    Yes, in that case you are initializing a to an empty array. And arrays are also first-class objects in JavaScript and can be assigned properties.

    Some people call JavaScript objects "associate arrays". Avoid that term. It's confusing and inaccurate.
    Rajesh Khan
    Ranch Hand

    Joined: Oct 16, 2011
    Posts: 230
    Its funny you mentioned that I just finished a book which was highly recommended for JS called "Javascript the definitive Guide" and the book actually states that objects are associative arrays.http://docstore.mik.ua/orelly/webprog/jscript/ch08_06.htm Initially i didn't quiet understand what they meant by that term but now I do. However ill avoid using that term for objects.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61310
        
      66

    That's a good book otherwise. But the term is inaccurate as "array" means certain things that aren't true about non-array objects.
     
    GeeCON Prague 2014
     
    subject: A bit of confusion regarding the function in javascript