view core-javascript/exercise-2.rst @ 213:0ea90a1819c2 default tip

Custom theming
author Radomir Dopieralski <radomir.dopieralski@allegro.pl>
date Wed, 27 Jun 2012 16:35:54 +0200
parents 58e4f484903f
children
line wrap: on
line source

Exercise two
============

Here's some more JavaScript code I found.  I've changed the names of
everything to hide the origin of the code.  But it's not something
I've made up.

.. literalinclude:: /core-javascript-work/exercise-2.js
   :language: javascript

The exercise is

#.  Rewrite the function copy_attributes so that it makes a loop over

    .. code-block:: javascript

        var keys = ['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg', 'hhh'];

#.  Write a copy_attributes_factory function so that we can write

    .. code-block:: javascript

       var copy_attributes = copy_attributes_factory(keys);

#.  Write a Fields class such that something like this will work:

    .. code-block:: javascript

      var aaa = Fields(keys);
      aaa.copy_attributes(src, tgt);


Hints
-----

#.  In JavaScript attribute access and item access are the same.
    First we use item access to set:

    .. code-block:: javascript

       js> a = {}
       [object Object]
       js> key = 'attrname'
       attrname
       js> a[key] = 42
       42

    and then we use attribute access to get:

    .. code-block:: javascript

       js> a.attrname
       42

#.  This applies even for function (aka method) calls.

#.  Have the factory function store the keys in a closure.

#.  There are many ways of writing Fields.  Choose one that suits you.