My understanding of the different kinds of JavaScript functions are as follows:
function MyObj() {
this.propOne = true;
this.publicInstanceFunc = function() {
if (propOne)
return 'public instance function';
}
function privateFunc() {
return 'private function only visible inside this constructor';
}
}
MyObj.prototype.protoFunc = function() {
if (this.propOne)
return 'prototype function shared amongst all instances of MyObj';
}
- Are these correct?
- In what cases should one put functions on the prototype (e.g.
protoFunc
) vs. in the constructor (e.g.publicInstanceFunc
)? - Is using
this
the correct way to access properties inside prototype functions?