Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

prototype question

 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do Function and Object have different prototype? thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object has no prototype.
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but I've seen quite a lot of snippet like object.prototype.whatever or Function.prototype.blah.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only if object is a function.
 
Hendra Kurniawan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can an object be a function? could you please explain more about this? thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JavaScript, functions are first class objects. So every function is an object.

Just like any other object type, functions can be:
  • Declared with literals.
  • Assigned to variables.
  • Passed as a parameter to a function.
  • Be returned as the value of a function.
  • And so on...
  •  
    Paul Clapham
    Sheriff
    Pie
    Posts: 20750
    30
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I thought it was about time for me to learn jQuery, so I started off by downloading it. However since I just clicked on the "Download" link, the code appeared in my browser. And right in the middle of the page I saw this:



    Probably there's more going on here than meets the eye, but possibly this sort of thing was what Hendra was asking about.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64623
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Get ready to have your mind blown: Object is a function! In fact, it's the constructor for instances of all things Object.

    In fact, anything that you can operate on with the new operator is a constructor function.

    Welcome to functional programming.

    When someone says that JavaScript != Java, few people have an inkling of how deep the differences go.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64623
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The promo for Secrets of the JavaScript Ninja isn't until early December, but anyone with these types of questions really needs to read chapters 3, 4, 5, and 6. They lay bare what it means for JavaScript to be a functional language, and what's really going on when we use functions and objects in JavaScript.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic