Javascript: where are the variables declared in an onload function attached?

advertisements

In code where a function is run at document.ready:

$(function() {
    var vm = new ViewModel();
    ko.applyBindings(vm);
});

Where can I find the variable vm under window in the hierarchy?


tl;dr: You can't access it from window, unless you put it there.

When you declare a variable inside a function, it is local variable scoped to the function, not a global variable, and is thus not available in the window object. You would need to set the variable as a property of window.

Example:

$(function() {
    var vm = new ViewModel();
    ko.applyBindings(vm);
    window.vm = vm;
});