Question

I am new to javascript, so forgive me for asking such a novice question. I have been trying to learn the javascript language by looking at SPA examples that use the following libraries durandal, knockout js & breeze js, sammy & require and am trying to use the Module pattern, I think. I have come across different code syntax as follows

define(['services/logger'], function (logger) {

    var model = "somedata"
    var vm = {
        activate: activate,
        title: 'Details View'
    };

    return vm;
    function activate() {
        logger.log('Details View Activated', null, 'details', true);
        return true;
    }
});

and

define(['services/logger'], function (logger) {

        var model = "somedata"

    return {
        activate: activate,
        title: 'Details View'
    };

    function activate() {
        logger.log('Details View Activated', null, 'details', true);
        return true;
    }
});

my questions are

  1. Is there an advantage to using one syntax over the over?
  2. Will the variable "model" in the code block above have a global scope?
  3. What is the best way to access the "title" variable from within functions? should I use this.title?

Thank you in advance.

Was it helpful?

Solution

  1. Syntax 2 is easier to read. In stead of assigning it to a variable and returning it straight away, it just returns the value. Other than that, there is no advantage.
  2. The "model" variable will not have global scope as it's defined in this specific context.
  3. on the object that is returned, you call title by objName.title

Does this answer your question?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top