Monday, January 17, 2011

JavaScript 101, Sign up task 2, Blog about what I've learned

So, I've just watched the first video, Javascript Basics - 1.  I'm actually probably going to watch it again as I write this blog post, since it was pretty dense, and a review would be good.  It turns out that I learned a lot, but am still confused on a number of issues, so this post is long.  A lot of it is simply paraphrasing(or sometimes quoting) the video itself, but that's being helpful in my parsing what I actually do and don't understand.  This particular post I don't see as having much use to anyone but me.


The intro tells a bit about Javascript, including a few things I knew, and a bunch of info I didn't know about the history of the language and where it fits into the wider computer universe.   I knew that Javascript was not Java, and had some notion about the name choice being to connect it to Java, but that's about it.  I also knew that Javascript was a complete language.

I learned that the standard for the language is very messy.  Which is a difficulty for a language.  I learned that Javascript is a 'functional language', akin to scheme.  Which is nice, because I've spent a bit of time learning a little tiny bit about scheme and I like the language structure(though the syntax drives me buggy).   I learned the convoluted and entertaining history of the language(as well as a bit about Java), and the origin of some of it's issues.  Particularly that Microsoft got involved and, as per their SOP, insisted that the language's standards remain bug compatible with previous versions, so as never to break any old programs.  This makes it difficult to fix certain intrinsic issues with the language.

Javascript was designed to be a lighter language than Java for use in webpages, with the syntax based on Java.  I'm actually not precisely certain what 'lighter' means in this context.  Looked that up, and it seems to mean run-time interpreted as opposed to compiled?  Like scheme, or my old friend Basic.  Might mean more.

Important bit:  All the keywords in the language are lowercase.  Some methods are 'camelcase', but generally all lowercase.  and it -is- case-sensitive.

Key ideas of the Language:

---  Load and go delivery
Basically as above, run-time interpreted, not compiled.  Sent to the client as text.

---  Loose typing
Any type can be stored in any variable, or passed as a parameter to any function, and any kind of value can be returned from a function.  I can see some uses to this, Lamda-wise.  I guess all the basic functions will return -some- value regardless of what type they are sent.  Which sounds like it could lead to some interesting errors.

---  Objects as general containers
Not certain.  Objects 'completely dynamic'.  Uncertain what this entails.

---  Prototypal Inheritance.
Also not certain what this means.  There are no classes of objects, which I kinda understand.  Objects inherit properties from other discreet objects.  I think?  Not really certain about this area.  More research needed.

---  Lambda
 Functions as first class objects.  Which I think means one can pass functions as variables.  Which is mathematically really nifty and what makes Lisp/Scheme cool.

---  Linkage through global variables
Separate compilation units are combined in a common global namespace.  Hmm.  Not sure how to parse that.  i know from talking with some friends that global variables have some serious security issues and vaguely comprehend some of why.  Does this mean that -all- variables in Javascript are global?  Are global variables shared between various Javascript programs?  That would be awful.


There are a small set of values... variable types?  Numbers, Strings, Booleans, Objects and the special values Null and undefined.  The speaker uses the term 'value' throughout the video.  I have to assume this includes variables but also includes in-line defined values.

Number is the only type of number-ish thing.  No Int.  Number is a 64-bit floating point(I know what that is).  Problem:  doesn't map well to common notion of arithmetic.  Which is odd.  It gives an 'approximation' of decimal fractions.  Which is...interesting.  not sure why this is done?  Maybe so that 1/3*3=1 instead of .999etc?  So doing arithmetic on decimal numbers often requires one to multiply till the number is an integer, then scale it back.  He talks specifically about dollars/cents, common decimal numbers that we would use arithmetic on.

Special number: NaN.  'Not a Number'.  Result of undefined or erroneous operations.  It's 'toxic', which I would more call 'contagious', any arithmetic with NaN as an input will have NaN as the output.  And it's not equal to anything, including NaN.  Nor <, nor > anything, including itself.  Which is...interesting.  Useful to keep in mind if boolean operations are throwing odd results.  Oh, and it actually -is- a Number.  It's type is Number anyway.

Number function:  Number(value).  Converts the value into a number.  By what formula it does this I am unaware, the video doesn't say.  I do know it fails sometimes and produces NaN.  Perhaps it changes strings that are, or contain, the character equivalent of a number into that number?  Questions, questions.  Oh and similar to the '+' prefix operator.

The whole ParseInt function kinda baffles me I have to say.  I don't know what the Radix argument is precisely.  It seems to force the number to conform to base ten as opposed to octal?  0 followed by something makes it an octal?  But...  why doesn't 08 return 10, since 8 is represented by 10 in octal?  Oh, wait, I get it...  the 0 tell it that it's octal, then the 8 is ignored, since it's not actually a number in the octal system.  Only numbers 0-7.  Ok, so I get it.  I assume that the second argument, if given, is the Radix argument.  Would have made sense to have 10 be the default value of radix, but....

So, ParseInt function converts a value specifically to an integer.  And can be used for octal numbers, and will do so automatically if the first char is 0.  The second argument in the function is the Radix, which should be defined as (10) if you want to remove this and force it to treat the number as an integer.

Separate math object.  Hmm.  I guess this is a math 'library' that includes some functions that do math.  Can make load lighter in the rare case that you have a program that doesn't want any math.  :P  Most useful possibly is Math.floor, which can be used to take the integer part of a number. 

Strings:  0 or more 16-bit char.  No specific 'char' type, that's simply a string with a length of 1.  An old type of unicode encoding, which differs slightly from the modern one, but rarely does that difference matter.  UCS-2 has no awareness of surrogate numbers, but that's ok because neither do I.

Strings are immutable, once it's been made it can't be modified.  Which sounds really odd.  To mess about with them does one has to create a whole new string?  Odd, but ok.   Similar strings are equal (==).  Meaning I guess if they have the same value they are considered == True.  " and ' are the same, either work.

String.length?  Not certain what's meant by this notation.  Is this a function?  Also, what's the difference between a function and a method?  Is this a function that returns the length or sets it?  Unclear.

String function turns a number into a string.  String(value).  I suppose it returns the char equivalent of the numbers.  So 108 becomes "108". 

A bunch of string methods that are listed.  Still not certain what a method is?  It's a function that an object inherits?

Boolean:  2 values only, True and False.  That's good.

Boolena function:  Boolean(value) returns true if 'truthy', false if 'falsey'.  Is similar to !! (not not, lol) prefix operator. Falsey values are: false, null, undefined, ""(empty string), 0 and NaN.  All other values(including all objects) are true, including the strings "0" and "false", since non-empty strings are true.

Null: a special value which isn't anything.  Sometimes useful?

Undefined: special value that is default and doesn't mean anything, even null.  Silly name, as something can be defined and have the value undefined.  Also...value you get when you try to extract a value from an object that doesn't have that value.  The 'missing value' value.

Everything else is objects.  Javascript is object oriented...?

So, objects.  Objects in Javascript are dynamic objects.  Unification of objects and hashtables would help if I knew what a hashtable was.  Grr.

new Object() creates an empty container of name/value pairs.  Ok.  So an object is a container of name/value pairs.  This... makes sense.  That makes for odd sorts of objects...?  This makes an odd language structure in my head.  The name can be any string, the value any value except undefined.   Once you make an object, you can add pairs to it.  Not sure quite how this is done?  Pairs referred to as 'members'.

Members can be accessed with either dot notation or subscript notation?  I assume dot notation is like the method notation(Object.Member), not sure what subscript notation is.

Although they are like hashes, there's no hash nature visible.  Since I don't know much about hashes, this is ok, I think?  Jscript doesn't even have hashtables....ok, dunno, that means sometimes goes slower on Jscript.  Need to learn more about hashes?

Javascript in the C family, syntactically, but can send functions as values.

Identifiers start with a letter, $ or _.  Followed by letters, $, _, 0, or other numbers.   By convention, all variables, parameter, members, and function names begin with lower case.  Except for  'constructors'(wazzat?), which start upper case.  Initial _ should be reserved for implementations and $ for machines, but not everyone does this.  Not precisely certain what either of those things are, but they aren't likely to be me anytime before I do know what they are, so that's ok.

Lots of reserved words that can't be used for other things.  Including a bunch that aren't used for anything, which is weird.  This also means (dot)names, like member names, cannot be reserved names.

Comments are pretty classic.  // for line comment, /*...*/ for block comment.

A bunch of basic arithmetic operators, similar to C.  But some differences.

+ used for both addition and concatenation.  If both are numbers, then add, otherwise turn them both to strings and concatenate.  This is something to watch for, order of operations ends up important.  A lot of mistakes come from this.  Also used as prefix operator to convert a string to a number, like the number(value) function.  +"42"=42.  Considering when used as basic operator it often converts numbers to strings, having it convert strings to numbers when alone is irritating.

/ of integers can produce slightly funky non-integers.  10 / 3 = 3.33...5?  Why 5?

So, == and != have an issue, they can force 'type coercion'.  === and !== do not.  Not entirely certain what type coercion is.  === and !== measure exact equality of value and type.  So I guess that equivalent strings and numbers will count as equal(true) with ==, and false with !=.   Not sure why these aren't described as boolean operators, given that they return boolean values?

Not certain I get &&, the 'guard operator'.  It's a 'logical and'.  It's really weird looking to me.  If first operand is truthy, then it returns the second operand, otherwise the second.  Huh?  Why is that &&?  I would think && would return true/false?  I guess only if the operands are boolean.  Otherwise it returns these funky other things.  Can be used to guard against null references, but I really don't get this bit?

|| makes more sense.  It's the opposite.  If they are both boolean, it works like a normal boolean.  Because if the first is true then it returns the first, otherwise the second.  This can also be used for default values.  Put the default in the second operand, and if the first is undefined, then it returns the default.  I can see uses for this.

! prefix logical not.  Produces reverse boolean.  !! is notnot, or produces boolean equivalent.

Bunch of 'bitwise' operators...  Not certain what bitwise means.  Converts to 32-bit signed integer, does logical operation, then turns back to a 64bit floating point.  What is bitwise?  Will have to look up.

And that appears to be it.  I'll make another post soonish that summarizes all the stuff I -didn't- learn.  but for now, tis enough, twill suffice.

1 comment:

  1. tnx for commenting at my blog. Hope you come back...

    dlw

    ReplyDelete