Presentation is loading. Please wait.

Presentation is loading. Please wait.

Javascript. Javascript Tools Javascript Console Javascript Console Debugger Debugger DOM Inspector DOM Inspector.

Similar presentations


Presentation on theme: "Javascript. Javascript Tools Javascript Console Javascript Console Debugger Debugger DOM Inspector DOM Inspector."— Presentation transcript:

1 Javascript

2 Javascript Tools Javascript Console Javascript Console Debugger Debugger DOM Inspector DOM Inspector

3 Numbers No real distinction between floating point and integers, internally, all numbers are fp No real distinction between floating point and integers, internally, all numbers are fp Octal integers begin with 0 and are followed by 0-7 (8,9 interpreted as decimals), these are not supported in the ECMA standard--you likely won't see an octal Octal integers begin with 0 and are followed by 0-7 (8,9 interpreted as decimals), these are not supported in the ECMA standard--you likely won't see an octal Hex integers begin with 0x, (0-9, a-f lower or uppercase), eg 0x10 is 16 in decimal Hex integers begin with 0x, (0-9, a-f lower or uppercase), eg 0x10 is 16 in decimal Octal and hex numbers can be negative, but cannot have a decimal portion nor support scientific notation. Octal and hex numbers can be negative, but cannot have a decimal portion nor support scientific notation. You're likely to run into issues mainly where you use an illegal number unknowingly…. You're likely to run into issues mainly where you use an illegal number unknowingly….

4 Invalid Numbers 00.234 (00 marks octal, which can't have a decimal) 00.234 (00 marks octal, which can't have a decimal) 0x3.45 (0x marks hex, and decimals aren’t allowed) 0x3.45 (0x marks hex, and decimals aren’t allowed) 0378 (value of 378 even tho marked as an octal, since 8 coerces interpretation as a decimal number) 0378 (value of 378 even tho marked as an octal, since 8 coerces interpretation as a decimal number)

5 Zero and Special Numbers Null is not a 0 (although it sometimes acts like it is Null is not a 0 (although it sometimes acts like it is Undefined values act differently (they are NAN, or Not a Number) Undefined values act differently (they are NAN, 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

6 Math 12how_old.html Standard Math functions supported (+, -, *, /, etc.) Standard Math functions supported (+, -, *, /, etc.) Use parseInt() or parseFloat() method to treat variables as numbers Use parseInt() or parseFloat() method to treat variables 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");

7 Coercion 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 exception If there's a conflict, javascript doesn't produce an exception string+number goes to strings string+number goes to strings boolean+string goes to strings boolean+string goes to strings number+boolean goes to numbers number+boolean goes to numbers Explicit conversions Explicit 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.

8 Coercion 15math_parse.html Javascript and typing can be a mess Javascript and typing can be a mess var x = prompt ("value of x",""); // use parseFloat() to convert x to a number var x_fp = parseFloat(x); document.write("x is has a type of " + typeof x + " "); + typeof x + " "); document.write("x_fp has a type of " + typeof x_fp + " "); + typeof x_fp + " ");</script>

9 Digression: Dealing with Forms In these recent examples, I've been prompting the user for data via an alert box--bad idea In these recent examples, I've been prompting the user for data via an alert box--bad idea You can use an HTML form instead--javascript can read and assign values to form objects You can use an HTML form instead--javascript can read and assign values to form objects You have to identify the objects, so name them You have to identify the objects, so name them

10 Math and Strings Like we saw in php, javascript's coercion can lead to errors Like we saw in php, javascript's coercion can lead to errors See math2.html and math3.html See math2.html and math3.html var x = prompt ("value of x",""); var y = prompt ("value of y",""); document.write("x * y = " + x*y + " "); document.write("x + y = " + x + y + " ");

11 Quick Review An object is just a collection of properties, values, and methods given a name An object is just a collection of properties, values, and methods given a name

12 Math Object Math is also an object Math is also an object document.write("The value of PI is: " + Math.PI + " "); + " "); document.write("A random number is: " + Math.random() + " "); + " "); document.write("The square root of 156 is: " + Math.sqrt(156) + " "); + Math.sqrt(156) + " "); document.write("The square root of 156 rounded is: " + Math.round(Math.sqrt(156)) + " "); + Math.round(Math.sqrt(156)) + " ");</script> 08Math_Object.html

13 Creating your own object You can create an object with an "array" of characteristics and values You can create an object with an "array" of characteristics 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 var obj = { name: "Carrot", name: "Carrot", "for": "Max", "for": "Max", details: { details: { color: "orange", color: "orange", size: 12 size: 12 } object.html

14 Arrays Javascript supports single dimensional arrays, and arrays of arrays Javascript supports single dimensional arrays, and 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!';


Download ppt "Javascript. Javascript Tools Javascript Console Javascript Console Debugger Debugger DOM Inspector DOM Inspector."

Similar presentations


Ads by Google