-5

I'm a seasoned Java developer by now, and I love how Java uses types, signatures, methods, etc. It makes everything understandable, easy to follow and logical.

Now I'm trying to develop something in Javascript. I'm trying to learn Angular JS. However I am finding it so very confusing to understand how this code is developed.

Take a look at the code at this tutorial about ng-repeat. I'll post a sampling of the code;

app.directive('lkRepeat', function(){
  return {
    transclude : 'element',
    compile : function(element, attr, linker){
      return function($scope, $element, $attr){
        var myLoop = $attr.lkRepeat,
            match = myLoop.match(/^\s*(.+)\s+in\s+(.*?)\s*(\s+track\s+by\s+(.+)\s*)?$/),
            indexString = match[1],
            collectionString = match[2],
            parent = $element.parent(),
            elements = [];

        // $watchCollection is called everytime the collection is modified
        $scope.$watchCollection(collectionString, function(collection){
          var i, block, childScope;

          // check if elements have already been rendered
          if(elements.length > 0){
            // if so remove them from DOM, and destroy their scope
            for (i = 0; i < elements.length; i++) {
              elements[i].el.remove();
              elements[i].scope.$destroy();
            };
            elements = [];
          }

My question is: How can you read such code? When functions are called, where are the method signatures? Also, when returning a value of function with parameters like $scope, $element, $attr, are these just place holders for the variables that will be passed? If so, then why do they have such funny names, why not drop the dollar sign?

CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
  • 3
    You shouldn't really dive into angular before understanding how javascript itself works - angular's documentation and its workflow presume a good handle on the language. – Sacho Oct 26 '14 at 10:14
  • `My question is: How can you read such code?` not suitable for stackoverflow network,maybe for code review – Kostia Mololkin Oct 26 '14 at 10:19
  • 2
    @KostiaMololkin — Code review is for questions of the nature "I have written this code. Could it be better?". It definitely isn't for this type of question. – Quentin Oct 26 '14 at 10:23

1 Answers1

0

When functions are called, where are the method signatures?

The closet JavaScript comes to having method signatures is that, when declaring a function, you can name the arguments.

function foo (named_argument, another_named_argument) {

Arguments are always optional. If you don't specify a value, it will get undefined.

You can specify as many arguments as you like and the function can access them through the arguments object.

Also, when returning a value of function with parameters like $scope, $element, $attr, are these just place holders for the variables that will be passed?

Yes

If so, then why do they have such funny names, why not drop the dollar sign?

The dollar sign is just a character that is allowed in a JavaScript identifier. Some people use it to indicate that the variable is expected to hold a jQuery object (because jQuery keys all its functionality off a variable called $).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Ok, but how does the author know that `compile:` should return a function that takes those three parameters? Who will be calling it? – CodyBugstein Oct 26 '14 at 10:17