Apr 29, 2015

Notes on object-oriented JavaScript for o-o developers (part 1)

I've started reading The Principles of Object-Oriented JavaScript by Nicholas C. Zakas.

So far, it's been excellent, one of the best technical books I've read. It's clear, concise, and doesn't have any fluff, a solid tutorial with fewer than one hundred pages.

Coming from having programmed a lot in the strongly-typed language C++ a couple of lifetimes ago, I find JavaScript very fluid and sometimes very different in certain aspects.

It has some commonalities with PHP, which is what I'm mostly working in these days and still learning.

The purpose of this post and its follow-up is to highlight those JavaScript features  described in this book that might be of particular interest to someone with a background similar to mine.



**  Javascript does not have classes nor class definitions in the usual sense.

It does have objects, which are instances of reference types. The built-in reference types include Object, Array, Date, Function, RegExp, and Error.


**  An object is a dynamic collection of properties and their values.

Properties can be added or removed from an object at any time. In other words, by default, an object is not constrained by whatever type was used to instantiate it.

For example, create a Date object, then add an arbitrary property to it. Note that assigning a value to a property that didn't exist before implicitly adds that property to the object.

  var date = new Date;
  date.blessing = 'May you live in interesting times';

Then delete the property. The date object is now back to its initial state.

  delete date.blessing;


**  The primitive types, which include string, number, and boolean,  sometimes can be treated as objects even though they are not objects.

Each of these primitive types has a corresponding primitive wrapper type, respectively, String, Number, and Boolean.

The JavaScript  interpreter creates temporary objects of these primitive wrapper types as needed, uses them, and then discards them immediately. This is how properties and methods can seemingly be invoked on primitive types.

For example, extract the first character of a string using a method call.

  var first = 'hello'.charAt(0);


**  Functions are first-class objects.

That is, functions are objects. So functions are values that can be assigned to variables, passed as parameters, be returned, and appear in expressions, just like values of other types.


**  There are two literal ways of defining functions.

The first is the familiar declaration that starts with the keyword function and must include a function name. For example,

  function helloWorld() {
    console.log("hello, world");
  }

The second way is an expression that also starts with the keyword function but does not include a name. For example, this code defines an anonymous function and assigns it to a variable.

  var helloWorld = function() {
    console.log("hello, world");
  }


**  A function declaration may appear after a call to that function.

For example, this is allowed.

  helloWorld();

  function helloWorld() {
    console.log("hello, world");
  }

The function declaration is effectively hoisted to the top of its context.

But this only works for function declarations, not for function expressions. The code below results in a runtime error.

  hello();

  var hello = function() {
    console.log("hello");
  }

(Continued at Part 2.)

Apr 25, 2015

Beta 9: automated tests, module_invoke(), and drush

When I tried to run the automated tests, none of them showed up in the list of available tests for the Testing module's configuration.

It turns out that there is a new requirement for writing test classes, namely,
Your test class needs a phpDoc comment block with a description and a @group annotation, which gives information about the test.
So, for example, by adding @group in the following comment block, the test class was recognized and listed under the Optimizely grouping. The lead paragraph of the comment block is treated as a description and is displayed accordingly in the listing.

/**
 * Create users with no, some, and optimizely permissions
 * to test access to module related pages.
 *
 * @group Optimizely
 */
class OptimizelyAccessTest extends WebTestBase { . . . . }



This @group annotation seems to have been created specifically for the Testing module and is not part of the general documentation standards.

Sources:

Automated tests
https://api.drupal.org/api/drupal/core!modules!system!core.api.php/group/testing/8

API documentation and comment standards
https://www.drupal.org/coding-standards/docs



Fatal error: Call to undefined function Drupal\optimizely\Tests\module_invoke()

Function module_invoke() has been removed. I replaced this call,

  $schema = module_invoke('optimizely', 'schema');

 with this one.

  $schema = \Drupal::moduleHandler()
              ->invoke('optimizely', 'schema');

Generally speaking, interface ModuleHandlerInterface and class ModuleHandler provide the functionality that was previously in functions such as this one as well as module_list() and module_implements() which have also been removed.

Sources:

Remove bootstrap.inc  module_invoke()
https://www.drupal.org/node/2330181

Module/hook system functions replaced with module_handler and module_installer
https://www.drupal.org/node/1894902 



Running drush on a freshly-installed Beta 9 site gave the error message, 

Drupal\Core\DependencyInjection\ContainerNotInitializedException: \Drupal::$container is not initialized yet. \Drupal::setContainer() must be called with a real container. in Drupal::getContainer() (line 129 of /var/www/html/opti/core/lib/Drupal.php).

I was using a version of drush 7 from January. Downloading and installing the current drush 7.0-dev  fixed this problem.

Sources:

Installing Drush for Drupal 8
http://optimizely-to-drupal-8.blogspot.com/2015/01/installing-drush-for-drupal-8.html

Apr 6, 2015

"An Introduction to Entities"

An Introduction to Entities
https://www.drupal.org/node/1261744

I might be particularly dense about this aspect of Drupal, but this is the first article I've read that clearly and succinctly describes the closely related concepts of entity type, bundle, node, content type, field, and entity.

For those who are familiar with object-oriented programming, there is a comparison that is helpful, even though the analogy is not a perfect match.

Kudos to the authors.