- methods are objects
This matter needs to be repeatedly emphasized. A method is an object of type Function, and like any other object, it has methods.
function gen() { return function ans(factor) { return 2 * factor; }; }
If it looks confusing, you can use variables to see
function gen() { var f = function ans(factor) { return 2 * factor; }; return f; }
or like this
function gen() { function ans(factor) { return 2 * factor; }; return ans; }
- How the method is named
Imagine you define the following method
function f(factor) { return 2 * factor; }
same as below
var f = function (factor) { return 2 * factor; };
f(2);
- way does not support polymorphism
You can't define two methods with the same name and hope to use different parameters to distinguish them. A method defined later overrides an earlier method.
function ans(f1, f2) { ... } function ans(f1) { ... } // This replaces the previous method.
It should be noted that all parameters are not required
function ans(a, b) { //... } ans(2); // ans is called with a = 2, and b = undefined
- function return
In the method definition you can return any value or nothing
function () { if (cond1) { // return an object t return { a: 10 }; } else if (cond2) { // return undefined return; } else if (cond3) { // return a number. return 1; } }