File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A bit of confusion regarding the function in javascript

 
Rajesh Khan
Ranch Hand
Posts: 230
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

  •  
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64178
    83
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 230
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 64178
    83
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 230
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 64178
    83
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 230
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 64178
    83
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's a good book otherwise. But the term is inaccurate as "array" means certain things that aren't true about non-array objects.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic