• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why function in javascript is treated as an Object

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,iam learning javascript would like to know why functions in Javascript are treated as Objects?

For example in java methods are just a group /a set of statement that can be reused but method is never treated as object.

But in javascrript function is treated as an object?

Is that because both java and javascript follow different programming model?

What is concept behind that treating functions as objects in javascrpt and why?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ShekarChandu Reddy wrote:Is that because both java and javascript follow different programming model?


Yes, Java and JavaScript have absolutely nothing to do with each other, despite the poor choice of naming.

With Java 8, which introduces some functional concepts, Java is moving towards being a functional language, but JavaScript has always been one.

What is concept behind that treating functions as objects in javascrpt and why?


Because JavaScript is a functional language.

Read Part II of Secrets of the JavaScript Ninja (2nd edition coming out soon!) for a detailed description of why functions are so important in JavaScript, and the advantages that being a functional language bring.

To summarize: it means that functions are values, and can be treated just like any other values: stored in variables, passed as parameters, and so on.
 
Shekar Chandu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, and would like to know one more thing

for function(or any variable) in javascript  to be qualified as an object does it has to strictly follow some rules?
Or in other words if some body asks me why functions in javascripts are object,i can say like java script follows Functional programming,so fuctions are objects
I mean how do you  say some thing is an object in javascript
I  read that every thing is an object in Javascript?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, functions are "first class citizens" in JavaScript. That means you can treat them like a 'thing'. All 'things' in JavaScript that are not primitives are objects, so functions are objects.

Most languages that have both functions and objects will treat a function as an object.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic