Upload
gregory-smith
View
277
Download
5
Embed Size (px)
Citation preview
JavascriptJavascript
Javascript ToolsJavascript Tools Javascript ConsoleJavascript Console DebuggerDebugger DOM InspectorDOM Inspector
NumbersNumbers No real distinction between floating point and integers, No real distinction between floating point and integers,
internally, all numbers are fpinternally, all numbers are fp Octal integers begin with 0 and are followed by 0-7 (8,9 Octal integers begin with 0 and are followed by 0-7 (8,9
interpreted as decimals), these are not supported in the interpreted as decimals), these are not supported in the ECMA standard--you likely won't see an octalECMA standard--you likely won't see an octal
Hex integers begin with 0x, (0-9, a-f lower or Hex integers begin with 0x, (0-9, a-f lower or uppercase), eg 0x10 is 16 in decimaluppercase), eg 0x10 is 16 in decimal
Octal and hex numbers can be negative, but cannot Octal and hex numbers can be negative, but cannot have a decimal portion nor support scientific notation.have a decimal portion nor support scientific notation.
You're likely to run into issues mainly where you use an You're likely to run into issues mainly where you use an illegal number unknowingly….illegal number unknowingly….
Invalid NumbersInvalid Numbers 00.234 (00 marks octal, which can't have 00.234 (00 marks octal, which can't have
a decimal)a decimal) 0x3.45 (0x marks hex, and decimals 0x3.45 (0x marks hex, and decimals
aren’t allowed)aren’t allowed) 0378 (value of 378 even tho marked as 0378 (value of 378 even tho marked as
an octal, since 8 coerces interpretation as an octal, since 8 coerces interpretation as a decimal number)a decimal number)
Zero and Special NumbersZero and Special Numbers Null is not a 0 (although it sometimes acts like it Null is not a 0 (although it sometimes acts like it
isis Undefined values act differently (they are NAN, Undefined values act differently (they are NAN,
or Not a Number)or Not a Number) +0 and -0 not the same+0 and -0 not the same +infinity and -infinity not the same+infinity and -infinity not the same
MathMath12how_old.html12how_old.html
Standard Math functions supported (+, -, *, /, Standard Math functions supported (+, -, *, /, etc.)etc.)
Use parseInt() or parseFloat() method to treat Use parseInt() or parseFloat() method to treat variables as numbersvariables as numbers
Precedence is important!Precedence is important!
var x = prompt ("What Year Is It?");var y = prompt ("What Year Were You Born","");document.write ("You're " + (parseInt(x)-parseInt(y)) + " years old");
CoercionCoercion A variable of one type can be used as if it were another. A variable of one type can be used as if it were another. If there's a conflict, javascript doesn't produce an If there's a conflict, javascript doesn't produce an
exceptionexception string+number goes to stringsstring+number goes to strings boolean+string goes to stringsboolean+string goes to strings number+boolean goes to numbersnumber+boolean goes to numbers Explicit conversionsExplicit conversions
string to an integer, use the parseInt method. string to an integer, use the parseInt method. string to a number, use the parseFloat method.string to a number, use the parseFloat method.
CoercionCoercion15math_parse.html15math_parse.html
Javascript and typing can be a messJavascript and typing can be a mess
<script type="text/javascript" language="JavaScript"><script type="text/javascript" language="JavaScript">var x = prompt ("value of x","");var x = prompt ("value of x","");// use parseFloat() to convert x to a number// use parseFloat() to convert x to a numbervar x_fp = parseFloat(x);var x_fp = parseFloat(x);document.write("x is has a type of " document.write("x is has a type of " + typeof x + "<br />");+ typeof x + "<br />");document.write("x_fp has a type of " document.write("x_fp has a type of " + typeof x_fp + "<br />");+ typeof x_fp + "<br />");</script></script>
Digression: Dealing with FormsDigression: Dealing with Forms In these recent examples, I've been prompting In these recent examples, I've been prompting
the user for data via an alert box--bad ideathe user for data via an alert box--bad idea You can use an HTML form instead--javascript You can use an HTML form instead--javascript
can read and assign values to form objectscan read and assign values to form objects You have to identify the objects, so name themYou have to identify the objects, so name them
Math and StringsMath and Strings Like we saw in php, javascript's coercion can Like we saw in php, javascript's coercion can
lead to errorslead to errors See math2.html and math3.htmlSee math2.html and math3.html
<SCRIPT LANGUAGE=JavaScript>var x = prompt ("value of x","");var y = prompt ("value of y","");document.write("x * y = " + x*y + "<BR>");document.write("x + y = " + x + y + "<BR>");</SCRIPT>
Quick ReviewQuick Review An object is just a collection of properties, An object is just a collection of properties,
values, and methods given a namevalues, and methods given a name
Math ObjectMath Object Math is also an objectMath is also an object
<script language="JavaScript" type="text/javascript"><script language="JavaScript" type="text/javascript">document.write("The value of PI is: " + Math.PI document.write("The value of PI is: " + Math.PI + "<br />");+ "<br />");document.write("A random number is: " + Math.random() document.write("A random number is: " + Math.random() + "<br />");+ "<br />");document.write("The square root of 156 is: " document.write("The square root of 156 is: " + Math.sqrt(156) + "<br />");+ Math.sqrt(156) + "<br />");document.write("The square root of 156 rounded is: " document.write("The square root of 156 rounded is: " + Math.round(Math.sqrt(156)) + "<br />");+ Math.round(Math.sqrt(156)) + "<br />");
</script></script>
08Math_Object.html
Creating your own objectCreating your own object You can create an object with an "array" of You can create an object with an "array" of
characteristics and valuescharacteristics and values We'll look at this in more detail later…We'll look at this in more detail later…
// Create an object named obj// Create an object named objvar obj = {var obj = { name: "Carrot",name: "Carrot", "for": "Max","for": "Max", details: {details: { color: "orange",color: "orange", size: 12size: 12 }}
object.html
ArraysArrays Javascript supports single dimensional arrays, Javascript supports single dimensional arrays,
and arrays of arraysand arrays of arrays
var phrases = new Array()phrases[1] = 'Hi there!';phrases[2] = 'Having fun?';phrases[3] = 'Glad to hear!';phrases[4] = 'Bubbadee, that\'s all folks.';phrases[5] = 'Well, maybe one more time!';