7 ms·
As v413 wrote, the main difference is how the function is linked to the instance. If you define a member function inside the constructor, that member function i
by solutionyogi 16y ago
As v413 wrote, the main difference is how the function is linked to the instance. If you define a member function inside the constructor, that member function is assigned to each and every object separately. The function code is not shared across all instances. This is extremely unintuitive if you are coming from a statically typed OOP language like Java/C#. To get a behavior similar to what Java/C# gives you, you should use prototype.
To demonstrate the concept, I created this code example:
http://jsbin.com/utuso3/edit http://jsbin.com/utuso3/edit [Click on 'Preview' button to run the example code]
I hope that made sense.
- flatline 16y agoThanks (and to v413), this does make sense. I hadn't thought it through that each call to the constructor will create a new function object, which was pretty apparent once stated and I re-examined the code.