Search code examples
javascriptvariablesknockout.jsjavascript-function-declaration

Function returning a variable without declared it


I recently search in the code of the library of knockout to find how observables are able to create dependencies with computed functions when we call it.

In the source code, I found the function linked to observables creation:

ko.observable = function (initialValue) {
    var _latestValue = initialValue;

    function observable() {
        if (arguments.length > 0) {
            // Write

            // Ignore writes if the value hasn't changed
            if (observable.isDifferent(_latestValue, arguments[0])) {
                observable.valueWillMutate();
                _latestValue = arguments[0];
                if (DEBUG) observable._latestValue = _latestValue;
                observable.valueHasMutated();
            }
            return this; // Permits chained assignments
        }
        else {
            // Read
            ko.dependencyDetection.registerDependency(observable); // The caller only needs to be notified of changes if they did a "read" operation
            return _latestValue;
        }
    }
    ko.subscribable.call(observable);
    ko.utils.setPrototypeOfOrExtend(observable, ko.observable['fn']);

    if (DEBUG) observable._latestValue = _latestValue;
    observable.peek = function() { return _latestValue };
    observable.valueHasMutated = function () { observable["notifySubscribers"](_latestValue); }
    observable.valueWillMutate = function () { observable["notifySubscribers"](_latestValue, "beforeChange"); }

    ko.exportProperty(observable, 'peek', observable.peek);
    ko.exportProperty(observable, "valueHasMutated", observable.valueHasMutated);
    ko.exportProperty(observable, "valueWillMutate", observable.valueWillMutate);

    return observable;
}

What I think is very weird is the returns of 'observable' where I don't found any declaration of this variable. Sure that great men who created this library don't forget to declared it.

How it is possible to use a variable without declared it and prevent it to be put in a global scope?

My feeling is we can used a function declaration as a variable when this function declaration is declared inside another function but I'm really not sure about how it works.

Edit: After searching on the web, I found this article.

In this article, the guy write this:

Use declarations, please "In the code of unexperienced developers, functions are often declared by expressions:

... code ... var f = function() { ... } ... Function Declarations are much more readable and shorter. Use them instead.

... code ... function f() { ... } ... Besides, functions declared this way can be called before it’s definition.

Use expressions only if you mean it. E.g for conditional function definition."

Ok, Am I an unexperienced developer? I don't think so. I just don't read all the odds of Javascript. :)


Solution

  • observable is a variable. It is declared by a function declaration.

    function observable() {
        ...
    }