Explicit use of JavaScript’s global object Friday, Jul 17 2009 

A lot of JavaScript programmers use an anonymous function at the top level, to establish a scope. This allows variables to be defined in a file (or module if your prefer), without adding anything to the global namespace.

The problem with this, however, it that it becomes awkward when you do need to add something to the global namespace.

Here’s a trick that solves this problem.

    (function()
    {
        var global = (function(){return this;}).call();
        global.Formula = function (){
            // body of function
        };
    })();

This code is equivalent to:

    var Formula;
    (function()
    {
        Formula = function (){
            // body of function
        };
    })();

Both forms have the same number of lines of code, but the first pulls ahead for each additional object added to the global namespace.  But there are other advantages.

  • It is made explicit, that the assignment to Formula is being made in the global namespace.
  • Suppose you decide that Formula and its companions should live in a different namespace.  In the first, simply search and replace on ‘global’ and replace the definition of global.
  • In the first, we take care to explicitly obtain the object we are changing, namely the global object.
  • According to Douglas Cockcroft, “JavaScript’s global object [...] is far and away the worst part of JavaScript’s many bad parts”. Writing as in the first example discourages and makes explicit the use of the global object.

Is JavaScript’s new syntactic sugar? Sunday, Jul 12 2009 

JavaScript has a ‘new’ operator for creating objects. Each object in JavaScript has a hidden pointer to a prototype object, from which it inherits properties. This pointer is set when the object is created, and cannot be changed during the lifetime of the object.

When ‘new’ is used, an object is created that has other than the usual default Object prototype object.

From ‘new’ we can define a function create(proto) that returns an object whose prototype object is ‘proto’. (I first saw this in Douglas Cockcroft’s book JavaScript: The Good Parts, but he calls it ‘beget’.)

Conversely, from ‘create’ we can define a function, say ‘New’ that has the same functionality as the ‘new’ operator. (In JavaScript ‘new’ is a keyword, whose meaning can be changed.)

The ‘create’ operator is in some sense simpler than the ‘new’ operator, but each can be defined in terms of the other. According to Wikipedia, syntactic sugar “is syntax designed to make things easier to read or to express, while alternative ways of expressing them exist”.

I think ‘create’ is simpler to use and understand than ‘new’, and so I don’t regard it as syntactic sugar for the creation creation of objects.  The same Wikipedia term also suggests the term  syntactic saccharin for gratuitous syntax which does not actually make programming easier.  I consider ‘new’ to be syntactic saccharin.

Javascript and skeletons Monday, Feb 9 2009 

Here’s an analogy (and I don’t know how useful it is). It’s about writing JavaScript for web pages.

A beetle, for example, has a hard external skeleton, to which everything else is attached. A cat, for example, has a hard internal skeleton, to which everything else is attached. A cat is soft outside (although with teeth and claws) while a beetle is soft inside.

So what’s this got to do with JavaScript and HTML? Well, a simple HTML page spruced up with some JavaScript for say form entry is a bit like a beetle, where the HTML is the hard external skeleton.

But a complex web page, constructed mostly from JavaScript and Ajax supplied data is something else. It’s too big and complex to be supported just by HTML and the DOM. It needs some sort of skeleton. And so perhaps should be built more like a cat.
(more…)

TUG board election Saturday, Jan 31 2009 

I’m standing for the board of the TeX Users Group. You’re invited to make comments on my election statement below.

I work for the Open University (the UK’s leading provider of distance education) as a TeX expert for print media. I’m also halfway
through a two-year project on putting mathematics on web pages.

In 2006–7 I set up MathTran, which now provides typesetting of
TeX-notation formulas to images as a public web service, serving
about a million images a month.

MathTran shows the value of TeX as a web service, which I’d like to
extend to whole documents. Installing and configuring TeX can be
slow and difficult. Using TeX through a web browser will help
beginners.

Part of my math-on-web project is a page where students can
interactively create a TeX-notation formula, say for putting on a web
page or in a word-processor document.

I have a doctorate in Mathematics and although not my career I still
have research interests. I have been using TeX for over 20 years,
and joined TUG in 1989. For the past two years I’ve been Chair of the
UK TeX Users Group, and have recently been re-elected for another
two years.

The past three years have seen UK TUG come out of a long period of
inactivity and decline. The credit for this of course belongs to the
Committee and the members, and not simply myself. We’ve organised
three successful meetings, adopted a new constitution, and set up a
website with links to UK TeX resources.

As a board member I would bring to TUG a focus on a key core
community, namely those who write material with lots of mathematics.
I have a particular interest in providing help and support,
particularly through web pages.

TUG, by virtue of TeX being a typesetting program, rightly has a
focus on print media. But to flourish we must also use new media
effectively. The Open University faces the same challenge, and my
experience there will help TUG.

TUG has a special responsibility, to publicise TeX and related
fonts, programs, documentation and other resources.

I’d like TUG to offer more to institutional members. In particular,
we should help them share user support experience and resources.
Supporting TeX can be daunting without outside help.

When I joined TUG there were over 150 institutional members. There are
now just 27. The loss I feel the most is the Library of Congress.

Implementing super in JavaScript Sunday, Sep 21 2008 

The problem

JavaScript has objects and what is called prototypal inheritance, but it does not have built-in support for many features provided by other languages. In particular, it does not have built-in support for classes and instances. Nor does it have built-in support for methods of one class to call super-class methods.

Here is an example. Class C is a subclass of class B, and both have an init method. We would like the init method of Class C to be able to call the init method of class B. In other words, when writing init for class C, we wish to call the super-class init method. Note that class B might in fact be a subclass of class A, and that B might inherit init from A, and not have an init method of its own.

This post of focussed in the single topic of providing access to super-class. It does not for instance cover how to provide support for class hierarchies and how to manage instance creation.

Motivation

Over the past few days I’ve been trying to understand John Resig’s post on Simple JavaScript Inheritance, on both the technical and the practical levels. John’s goal was to extract the soul of the implementations of classical inheritance in base2 and Prototype, and to present them as a stand-alone package.

Well, I’ve not succeeded, but I do understand the problem better. In particular, I couldn’t get a clear understanding of what his _super method did. So to help me understand I set out to write my own. And this is what I came up with.

I hope in a later post to compare the two approaches. Be warned that this post is unavoidable quite technical, and that if you’re a JavaScript novice you’ll learn a lot if you manage to understand it all.

(more…)

Data objects in JavaScript Saturday, Sep 20 2008 

The Problem

Suppose we want to count the words in a document, or the identifiers in a computer program. We’ll need to keep a dictionary (aka hash or mapping) whose keys are the words and whose values are their frequencies. When we come across a new word (or identifier) we add it to the dictionary as a key, with value equal to one. When we meet a word already in the dictionary, we increase the value by one.

The starting point, of course, is an empty dictionary (or mapping or hash or whatever you want to call it). In JavaScript creating one of these requires some thought, and seems not to have been done before. Here’s the problem. In JavaScript we have to store the data (key-value pairs) in an object, and every object comes with some builtin methods. Here’s some command-line JavaScript:

js> data = {};  // Create empty data object, we hope.
js> data.foo === undefined; // true
js> data['foo'] === undefined;  // true
js> data['constructor'] === undefined; // false - not what we want.

(more…)

Learning jQuery Sunday, Apr 27 2008 

Learning from mistakes

This post is about my experience of learning jQuery. I’m developing some JavaScript for use with MathTran, and from past experience I know that I need a library. Elsewhere I explain why I’ve chosen jQuery. On Friday I started learning jQuery, mostly from the two Packt books, and yesterday and today (Saturday and Sunday) I’ve started with some coding.

I’m fairly new to JavaScript programming, and it’s easy for me to trip up on some of the language features. For example, it’s my Python habit to write ’self’ when I really should be saying ‘this’. I addition, I’m not used to the widespread dynamic creation of functions, to be attached to DOM elements to handle events. So in addition to learning how to think jQuery, I’ve also been learning how to think JavaScript and DOM.

(more…)

Using MathTran in blogs and wikis Wednesday, Feb 27 2008 

This post is mainly for system administrators and developers who want to add mathematics capabilities to a blog or wiki that they host or develop. We hope that in a year or so some of the ideas we describe here will be available to ordinary users who have a blog, say on WordPress.

After an Introduction, we discuss Light-, Medium- and Heavy- weight installation of MathTran. Then we discuss how to print mathematics using MathTran. Finally, we revisit the examples in the introduction, and state conclusions.

(more…)

Microsoft loves Yahoo … Saturday, Feb 9 2008 

… but will they be happy together?

Microsoft loves Yahoo, but will they be happy together? To find out visit the LoveGraph page for this match.

LoveGraph uses a special algorithm to generate data about the potential of the match, which it then graphs. Do pay particular attention to the “Spheres of Interest Overlap” chart.

LoveGraph uses charts and graphs generated by Google Charts.

(more…)

Django and the MathTran website – AJAX and templates Saturday, Feb 2 2008 

Introduction

In my previous post I spoke about ReST. It’s not able to do what I want, which is a nuisance. But most of the post of about coding forms such as that on the MathTran home page using Django templates. In the course of the post I come to the conclusion that providing such forms is a matter just for the template, and has nothing much to do with the view! Finally, I make some remarks about AJAX, which is used (along with some rather improper dynamic HTML) in the present implementation.

(more…)

Next Page »