The javascript language

Table of Contents

last revision
27 October 2011, 6:33pm
book quality
poor

,

I learnt javascript before the advent of ajax and was thinking about writing another vim in javascript. A truly strange project, but hence this book.

Variables ‹↑›

clear a variable

 lastinsert = '';

Arrays ‹↑›

define a new array

 var a = new Array();

get an array of all <a> html elements

 var d = document.getElementsByTagName('A');

set a array element to be an anonymous function

 this.movements['E'] = function(){this.endOfWord('E');}

set an array to a method this.movements['H'] = this.topOfScreen;

String Variables ‹↑›

get the length of a string

 var i = s.length;

a function to check if a string contain a space character

 function hasSpace(s) { return (s.indexOf(' ') == -1) ? false : true; }

Substituting And Replacing In Strings ‹↑›

replace a pattern in a string and assign the result

 var t = s.replace(/[.?!,:]*$/,"");

Matching Patterns ‹↑›

check if a string matches a pattern

 if (s.match(/^[ \r\n\t]*<.*>[ \r\n\t]*$/)) return true

If Statement ‹↑›

 if (z == -1) { x++; }

Ternary Operator ‹↑›

 var d = (z > 2) ? -1 : 1;

Functions ‹↑›

define a function called 'f'

 function name( f ) { return f; }

Objects ‹↑›

It is possible to program in an object oriented style with javascript

define a constructor

    function Editor(s)
    {
      this.message = s;
    }

define a new class "Editor" with fields and methods

   Editor.prototype = 
   {
     text: new Array(),
     movements: new Object(),
     nextChar: function()
     {
       return this.text[this.cursorRow].charAt(this.cursorCol);
     },
        ...
   };

DOCUMENT-NOTES: