Prototype
Most of the Javascript code are objects, but interestingly Javascript do not have class concept. Instead it will use prototype
.
When we define a constructor function below like that, an object prototype
is created. Anything defined in prototype
can be accessed by all the instances.
var Object = function(a, b) {
this.attributeA = a;
this.attributeB = b;
}
When we define a method within prototype
, it can be access by all the instances (they are objects as well, so confusing). And it’s static as well.
Object.prototype.methodA = function() {
// Logic goes here.
}
More details please check this Codepend.
Callback function
callback
is used very often in Javascript. callback
decouple the host and callback function itself. We could use it in these senario:
- Provide a interface to be invoked by different objects.
- Pass value to callback function on the fly/dynamically.
- Event driven - execute only when the event triggered.
Here’s a Codepen about callback function.
Hoisting
Mis-understanding hoisting
will cause some error difficult to detect.
And I think this concept is quite unique/ odd behaviours exist in Javascript ONLY.
This is a stackoverflow link have sample and detail explanation about hosting
.
Recursive function
This is a recursive function experiment.