[Mikeb's notes on JS Tony Garnock-Jones **20050905125941] { hunk ./Notes.html 41 -TiddlyWiki +KeepingWebsitesAlive hunk ./Notes.html 4237 -
a reusable non-linear personal web notebook
-
http://www.tiddlywiki.com/
-
TiddlyWiki
+
<<option chkOpenInNewWindow>> OpenLinksInNewWindow\n<<option chkSaveEmptyTemplate>> SaveEmptyTemplate\n<<option chkToggleLinks>> Clicking on links to tiddlers that are already open causes them to close\n^^(override with Control or other modifier key)^^\n<<option chkHttpReadOnly>> HideEditingFeatures when viewed over HTTP\n<<option chkSaveLexicalOrder>> SaveInLexicalOrder\n
hunk ./Notes.html 4239 -
From http://www.lshift.net/:\n\n"LShift is a pure technology company with experience of developing complex solutions for large companies.\n\n"Our sole aim is to deliver the most suitable technology for your business. We don't just look at the technical brief, we look at the overall solution. We achieve our high standards by hiring only the best & most experienced people in the industry.\n\n"In short, LShift will offer you a speed and quality of technical solution you won't be able to find elsewhere."
-
\n[[Introduction]]\n\nTiddlyWiki\nTonyGarnockJones\nLShift\n\n<<newTiddler>>\n<<newJournal "DD MMM YYYY">>
hunk ./Notes.html 4240 +
These are some notes on JavaScript and programming in general, originally written by MichaelBridgen for 2004's course.\n\n!! Programming languages\n\nA programming language is just a code for doing calculations, like\nmathematical notation. An //interpreter// takes the code as input and\nperforms the calculation. Like mathematical notation, the code has to\nbe concise and unambiguous.\n\nThe set of rules governing the form that the code takes is the\n//syntax//. How the code tells the interpreter to behave is the\n//semantics//. A lot of the difference between programming languages\nis in the syntax, rather than the behaviour.\n\n!! Expressions\n\nAs an example of a calculation, try\n\n{{{\n4 + 5;\n}}}\n\n(the semi-colon at the end signals the end of a calculation). The\nJavaScript interpreter returns the obvious result:\n\n=> //9//\n\nIf you input numbers, the interpreter will treat them as numbers (this\nworks for real numbers as well). You can also enter pieces of text in\nquote marks, like\n\n{{{\n"here is some text"\n}}}\n\n'Adding' strings (of characters) concatenates the pieces of text;\n\n{{{\n"here is " + "some text";\n}}}\n\n=> //"here is some text"//\n\nThere are other operators, like multiplication (an asterisk), division\n'/' and exponentiation '^'. You can use parentheses to enforce\nprecedence, just like in arithmetic:\n\n{{{\n4 * (5 + 2);\n}}}\n\nYou can mix up types of value, and the interpreter will try its best\nto give you a result:\n\n{{{\n"what is the result? " + 4;\n}}}\n\nThe interpreter will usually return //undefined// if it can't\ncalculate what you're asking.\n\n!! Names for things\n\nSo that we don't have calculate an expression more than once, we can\nname the result to be used later on. In JavaScript, the keyword\n''var'' introduces a name for something and the equals sign links\nthe name to the value; e.g.,\n\n{{{\nvar intermediate = 4 + 5;\n}}}\n\nWe can now use the name in the place of the value in later\ncalculations:\n\n{{{\nintermediate * 2;\n}}}\n\n=> //18//\n\n!! Logical expressions\n\nAs well as using arithmetic, JavaScript also has comparison operators\nlike ''<'', ''>'', ''=='' (note the double equals for //comparing// values).\nThe result of a comparison is a ''boolean'' value - ''true'' or\n''false''. So, for instance,\n\n{{{\n"this string" == "this string" ;\n\n"this string" == "that string" ;\n}}}\n\n=> //true//\n\n=> //false//\n\nFor combining boolean values, there are the operators 'AND' (''&&'') and\n'OR' (''||''). With a bit of experimentation we can see how these\nbehave:\n\n{{{\ntrue && true ;\n\ntrue && false ;\n\nfalse && true ;\n\nfalse && false ;\n}}}\n\nSo, the value of an AND expression is true only if both the operands\nare true.\n\nThe value of an OR expression is true if //either// (or both) of the\noperands are true.\n\nWe can use boolean values to make decisions in expressions.\nJavaScript has the ''ternary operator'' for this purpose:\n\n{{{\n( <boolean-expression> ) ? <if-true> : <if-false> ;\n}}}\n\nJavaScript first evaluates boolean-expression; if the result is\n''true'', the value of the entire expression is the value of\nif-true; otherwise, it is the value of if-false. For example,\ndetermining the plurality of a noun:\n\n{{{\n( numberOfMice > 1 ) ? "mice" : "mouse" ;\n}}}\n\n\n!! Procedures\n\nNot only can we name values, but we can name calculations. There is a\nspecial piece of syntax for making a procedure in JavaScript:\n\n{{{\n function ( parameters ) { return result; }\n}}}\n\nWe can give a name to a function:\n\n{{{\n var square = function (x) { return x * x; } ;\n}}}\n\nThe parameters are names that you supply values for when you apply the\nfunction. This function reads as 'a procedure in which, when given\nsome value x, the result is x times x'. The //x// is a placeholder\nfor the value.\n\nNow we can apply the function by supplying a value in the place of the\nparameter //x//:\n\n{{{\n square (6);\n}}}\n\n=> //36//\n\nThe value of a function application such as above is the function\nbody evaluated with the placeholder //x// replaced by the value we\ngave: //6//.\n\nWe can also supply an expression as the value of the parameter:\n\n{{{\n square (4 + 5);\n}}}\n\n=> //81//\n\n!! //Aside//\n\nThere are two ways JavaScript can evaluate //square(4 + 5);//\nFirstly, it could replace every mention of the parameter //x// with\nthe expression given:\n\n//square(4 + 5)//\n\n-> //(4 + 5) * (4 + 5)//\n\n-> //81//\n\nthen evaluate the entire expression; or, it can evaluate the argument,\nthen do the replacement, then evaluate:\n\n//square(4 + 5)//\n\n-> //square(9)//\n\n-> //9 * 9//\n\n-> //81//\n\nThe first is called ''Normal order'' and the second ''Applicative order''.\nJavaScript uses applicative order. The distinction isn't important\nunless, as is the case with JavaScript, it's possible there are side\neffects from evaluating an expression. With normal order, you can see\nthat the expression is effectively evaluated twice, which means any\nside-effects would also happen twice.\n\n!! Scope\n\nYou may have wondered, "what if I've already used 'x' as a name?".\nJavaScript handles this issue by having the notion of 'scope'. Making\na function introduces a scope, whereby the parameter names are\n//local// and only defined inside the function. Variables defined\ninside the function are also //local// to the function.\n\nWhen referring to a name, the name used is the one defined in the\nnearest scope //in the code//. For example, in\n\n{{{\n var x = 8;\n square = function (x) { return x * x; } ;\n square (4);\n}}}\n\n=> //16//\n\nthe x inside the function takes the value of //4//, not //8//,\nwhen //square// is applied to //4//. We can replace 'x' in the\nfunction parameters and body with 'y' and not change the result:\n\n{{{\n var x = 8;\n var square = function (y) { return y * y; };\n square (4);\n}}}\n\n=> //16//\n\n{Write a function called 'abs' that returns the absolute value of a number.}\n\n{{{\nvar abs = function (n) {\n return (n < 0) ? - n : n ;\n} ;\n}}}\n\n{Write a function 'average' that returns the average of two numbers.}\n\n{{{\nvar average = function (n, m) {\n return (n + m)/2 ;\n} ;\n}}}\n\n{Write a function 'max3' that returns the largest of three numbers.}\n\n{{{\nvar max3 = function (x, y, z) {\n return (x > y) ? ((x > z) ? x : z) : (( y > z) ? y : z) ;\n} ;\n}}}\n\n!! Function example\n\nHere I'm going to use the example given in [SICP]: Finding square roots using Newton's Method.\n\nFirst, we'll describe the solution in words, then transcribe it to JavaScript.\n\n1. For a number x, make an initial guess at the square root, y\n2. see how close it is; if close enough, we have our answer\n3. if not close enough, find a better guess by averaging y and x/y\n\nFor a start, we have to define what 'close enough' is. We'll use a tolerance:\n\n{{{\nvar TOLERANCE = 0.0001 ;\nvar closeEnough = function (guess, x) { return (abs(square(guess) - x )) < TOLERANCE ; } ;\n}}}\n\nNow, we encode the procedure for making a better guess:\n\n{{{\nvar betterGuess = function (guess, x) { return average(guess, x / guess) ; } ;\n}}}\n\nand finally, our solution:\n\n{{{\nvar sqrtIter = function (guess, x) {\n return (closeEnough(guess, x)) ?\n guess :\n sqrtIter(betterGuess(guess, x), x) ; } ;\nvar sqrt = function (x) { return sqrtIter(1.0, x); } ;\n}}}\n\n{Using an absolute tolerance doesn't work well with small or very large numbers. Rewrite the functions above so that the "good enough" criterion is that successive guesses are close to each other}\n\nNote that we can define values inside function definitions, so that we can 'hide' the auxiliary procedures inside the definition of //sqrt//; after all, the //sqrt// function is the only one we really care about being available to other functions. Since the //x// is now //in scope//, we don't need to include it as a parameter.\n\n!! Recursion and Iteration\n\nIn the previous examples, the function //sqrtIter// was used in its own definition. This is worth looking at further, but we'll use a more simple example: computing a factorial.\n\nThe factorial of a number //n// is denoted by ''n!'' and is n * (n - 1) '' ... '' 1; that is, the product of all whole numbers between 1 and n. Factorials are used in, among other things, determining permutations and combinations; e.g., calculating the answers to questions like "how many ways can I choose 6 from 40 lottery balls".\n\nThe procedure for generating a factorial is suggested by the definition above, along with observing that 1! is just 1:\n\n{{{\nvar factorial = function(n) { return (n == 1) ? 1 : n * factorial(n-1) ; };\n}}}\n\nAnother way of calculating the factorial is to hold values for the result so far, and the number we are up to, which we increment:\n\n{{{\nvar factorialIter = function(n, res, i) { return (i > n) ? res : factorialIter(n, res * i, i+1) ; } ;\nvar factorial = function(n) { return factorialIter(n, 1, 1) ; } ;\n}}}\n\nThese give the same results, but if we simulate them using our subsitution principle of earlier, we can see that the first 'expands' while it waits for the function applications to be evaluated, whereas the second can evaluate the function arguments straight away.\n\n{The Fibonacci sequence goes 1, 1, 2, 3, 5, 8, ..., (f-1)+(f-2). Write a recursive function to calculate a given member of the sequence}\n\n{Adapt your function to be iterative, if it isn't already}\n\n!! Special forms\n\nThe iterative factorial procedure above holds onto all its state as parameters; at any point, the arguments represent all the information necessary to continue with the calculation. This pattern is so common that JavaScript has special syntax for describing iterative processes.\n\n{{{\nfor (initial ; condition ; update) { ... } ;\ne.g.., for (var i=0; i < 10; i++) { ... } ;\n}}}\n\nHere the variable //i// is initally given the value 0, and the body (...) is repeated, with //i// being incremented each time, until the condition //i < 10// fails. The statements in the body may make use of //i//; our factorial example above becomes\n\n{{{\nvar factorial = function(n) {\n var result = 1;\n for (var i=0 ; i < n ; i++) {\n result = result * i ;\n }\n return result;\n} ;\n}}}\n\nYou can also see that it is possible to change values after they have been defined. In fact, in some programming languages, this is pretty much the only way to get things done!\n\nThere's also a shortcut form for defining functions:\n\n{{{\nfunction <name> ( <parameters> ) { <body> } // body can have more than one statement\n}}}\n\nThe longer conditional form is if:\n{{{\nif ( <condition> ) { <true-body> } else { <false-body> }\n // the bodies may be more than one statement\n}}}\n\n!! References\n\n''[SICP]'' Structure and Interpretation of Computer Programming, available online\nat\nhttp://mitpress.mit.edu/sicp/full-text/book/book-Z-H-4.html#%//toc//start\nis an excellent introduction to programming using the language Scheme.\nIn fact, I've adapted most of the content here from the first\nchapter(s) of SICP.\n\n''[NSJS]'' Netscape JavaScript Reference. A sturdy reference for the JavaScript language - after all, Netscape invented it. Found at http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/\n
+
From http://www.lshift.net/:\n\n"LShift is a pure technology company with experience of developing complex solutions for large companies.\n\n"Our sole aim is to deliver the most suitable technology for your business. We don't just look at the technical brief, we look at the overall solution. We achieve our high standards by hiring only the best & most experienced people in the industry.\n\n"In short, LShift will offer you a speed and quality of technical solution you won't be able to find elsewhere."
+
\n[[Introduction]]\nJavaScript\n\nTiddlyWiki\nTonyGarnockJones\nMichaelBridgen\n\nContent © 2005 LShift.net\n\n<<newTiddler>>\n
+
// Make the order of tiddlers when saved a configurable option\n\nconfig.options.chkSaveLexicalOrder = true;\nloadOptionsCookie();\n\nfunction allTiddlersAsHtml()\n{\n var savedTiddlers = [];\n var orderBy = (config.options.chkSaveLexicalOrder) ? "title" : "modified";\n var tiddlers = store.getTiddlers(orderBy);\n for (var t = 0; t < tiddlers.length; t++)\n savedTiddlers.push(tiddlers[t].saveToDiv());\n return savedTiddlers.join("\sn");\n}\n
+
notes on ContentManagementSystems
+
KeepingWebsitesAlive
+
http://www.tiddlywiki.com/
hunk ./Notes.html 4248 -
// Make the order of tiddlers when saved a configurable option\n\nconfig.options.chkSaveLexicalOrder = false;\nloadOptionsCookie();\n\nfunction allTiddlersAsHtml()\n{\n var savedTiddlers = [];\n var orderBy = (config.options.chkSaveLexicalOrder) ? "title" : "modified";\n var tiddlers = store.getTiddlers(orderBy);\n for (var t = 0; t < tiddlers.length; t++)\n savedTiddlers.push(tiddlers[t].saveToDiv());\n return savedTiddlers.join("\sn");\n}\n
-
<<option chkOpenInNewWindow>> OpenLinksInNewWindow\n<<option chkSaveEmptyTemplate>> SaveEmptyTemplate\n<<option chkToggleLinks>> Clicking on links to tiddlers that are already open causes them to close\n^^(override with Control or other modifier key)^^\n<<option chkHttpReadOnly>> HideEditingFeatures when viewed over HTTP\n<<option chkSaveLexicalOrder>> SaveInLexicalOrder\n
}