I’m learning javascript and one thing I keep hearing is “functions are objects”. Can you provide some examples for me to better understand this concept?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Sure. Here’s some example code:
Here’s proof that it works, too.
Notice how you can assign properties to the function, and assign it to itself and refer to it recursively. It’s an object, like most other things in JavaScript, and you can do anything with it. If you’ve seen prototype inheritance before:
There’s a lot here – a function literal, which is a function without a name in essence; prototypical inheritance; methods on functions; dynamic arguments; a function as
this.