Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

JavaScript: A Beginner's Guide to Understanding the Differences with Java and Fundamentals, Slides of Computer Science

An introduction to javascript, explaining its relationship to java, its syntax similarities and differences, and its usage in html documents. It covers javascript's primitive data types, operators, comments, statements, exceptions, object literals, and arrays. Additionally, it discusses the three ways to create an object and the use of the for...in statement.

Typology: Slides

2012/2013

Uploaded on 03/19/2013

dharamnishth
dharamnishth 🇮🇳

2.5

(2)

54 documents

1 / 29

Toggle sidebar

Related documents


Partial preview of the text

Download JavaScript: A Beginner's Guide to Understanding the Differences with Java and Fundamentals and more Slides Computer Science in PDF only on Docsity! JavaScript Language Fundamentals Docsity.com About JavaScript • JavaScript is not Java, or even related to Java – The original name for JavaScript was “LiveScript” – The name was changed when Java became popular – Now that Microsoft no longer likes Java, its name for their JavaScript dialect is “Active Script” • Statements in JavaScript resemble statements in Java, because both languages borrowed heavily from the C language – JavaScript should be fairly easy for Java programmers to learn – However, JavaScript is a complete, full-featured, complex language • JavaScript is seldom used to write complete “programs” – Instead, small bits of JavaScript are used to add functionality to HTML pages – JavaScript is often used in conjunction with HTML “forms” • JavaScript is reasonably platform-independent Docsity.com Where to put JavaScript • JavaScript can be put in the <head> or in the <body> of an HTML document – JavaScript functions should be defined in the <head> • This ensures that the function is loaded before it is needed – JavaScript in the <body> will be executed as the page loads • JavaScript can be put in a separate .js file – <script src="myJavaScriptFile.js"></script> – Put this HTML wherever you would put the actual JavaScript code – An external .js file lets you use the same JavaScript on multiple HTML pages – The external .js file cannot itself contain a <script> tag • JavaScript can be put in an HTML form object, such as a button – This JavaScript will be executed when the form object is used Docsity.com Primitive data types • JavaScript has three “primitive” types: number, string, and boolean – Everything else is an object • Numbers are always stored as floating-point values – Hexadecimal numbers begin with 0x – Some platforms treat 0123 as octal, others treat it as decimal • Since you can’t be sure, avoid octal altogether! • Strings may be enclosed in single quotes or double quotes – Strings can contains \n (newline), \" (double quote), etc. • Booleans are either true or false – 0, "0", empty strings, undefined, null, and NaN are false , other values are true Docsity.com Variables • Variables are declared with a var statement: – var pi = 3.1416, x, y, name = "Dr. Dave" ; – Variables names must begin with a letter or underscore – Variable names are case-sensitive – Variables are untyped (they can hold values of any type) – The word var is optional (but it’s good style to use it) • Variables declared within a function are local to that function (accessible only within Docsity.com Comments • Comments are as in C or Java: – Between // and the end of the line – Between /* and */ • Java’s javadoc comments, /** ... */, are treated just the same as /* ... */ comments; they have no special meaning in JavaScript Docsity.com Statements, I • Most JavaScript statements are also borrowed from C – Assignment: greeting = "Hello, " + name; – Compound statement: { statement; ...; statement } – If statements: if (condition) statement; if (condition) statement; else statement; – Familiar loop statements: while (condition) statement; do statement while (condition); Docsity.com Statements, II • The switch statement: switch (expression) { case label : statement; break; case label : statement; break; ... default : statement; } • Other familiar statements: – break; – continue; – The empty statement, as in ;; or { } Docsity.com Exception handling, II • try { statements to try } catch (e if test1) { exception handling for the case that test1 is true } catch (e if test2) { exception handling for when test1 is false and test2 is true } catch (e) { exception handling for when both test1and test2 are false } finally { // optional, as usual code that is always executed } • Typically, the test would be something like e == "InvalidNameException" Docsity.com Object literals • You don’t declare the types of variables in JavaScript • JavaScript has object literals, written with this syntax: – { name1 : value1 , ... , nameN : valueN } • Example (from Netscape’s documentation): – car = {myCar: "Saturn", 7: "Mazda", getCar: CarTypes("Honda"), special: Sales} • The fields are myCar, getCar, 7 (this is a legal field name) , and special • "Saturn" and "Mazda" are Strings • CarTypes is a function call • Sales is a variable you defined earlier – Example use: document.write("I own a " + car.myCar); Docsity.com Three ways to create an object • You can use an object literal: – var course = { number: "CIT597", teacher: "Dr. Dave" } • You can use new to create a “blank” object, and add fields to it later: – var course = new Object(); course.number = "CIT597"; course.teacher = "Dr. Dave"; • You can write and use a constructor: – function Course(n, t) { // best placed in <head> this.number = n; // keyword "this" is required, not optional this.teacher = t; } – var course = new Course("CIT597", "Dr. Dave"); Docsity.com The length of an array • If myArray is an array, its length is given by myArray.length • Array length can be changed by assignment beyond the current length – Example: var myArray = new Array(5); myArray[10] = 3; • Arrays are sparse, that is, space is only allocated for elements that have been assigned a value – Example: myArray[50000] = 3; is perfectly OK – But indices must be between 0 and 232-1 • As in C and Java, there are no two-dimensional arrays; but you can have an array of arrays: myArray[5][3] Docsity.com Arrays and objects • Arrays are objects • car = { myCar: "Saturn", 7: "Mazda" } – car[7] is the same as car.7 – car.myCar is the same as car["myCar"] • If you know the name of a property, you can use dot notation: car.myCar • If you don’t know the name of a property, but you have it in a variable (or can compute it), you must use array notation: car["my" + "Car"] Docsity.com Array functions • If myArray is an array, – myArray.sort() sorts the array alphabetically – myArray.sort(function(a, b) { return a - b; }) sorts numerically – myArray.reverse() reverses the array elements – myArray.push(…) adds any number of new elements to the end of the array, and increases the array’s length – myArray.pop() removes and returns the last element of the array, and decrements the array’s length Docsity.com Functions • Functions should be defined in the <head> of an HTML page, to ensure that they are loaded first • The syntax for defining a function is: function name(arg1, …, argN) { statements } – The function may contain return value; statements – Any variables declared within the function are local to it • The syntax for calling a function is just Docsity.com Regular expressions • A regular expression can be written in either of two ways: – Within slashes, such as re = /ab+c/ – With a constructor, such as re = new RegExp("ab+c") • Regular expressions are almost the same as in Perl or Java (only a few unusual features are missing) • string.match(regexp) searches string for an occurrence of regexp – It returns null if nothing is found – If regexp has the g (global search) flag set, match returns an array of matched substrings – If g is not set, match returns an array whose 0th element is the matched text, extra elements are the parenthesized subexpressions, and the index property is the start position of the matched substring Docsity.com Warnings • JavaScript is a big, complex language – We’ve only scratched the surface – It’s easy to get started in JavaScript, but if you need to use it heavily, plan to invest time in learning it well – Write and test your programs a little bit at a time • JavaScript is not totally platform independent – Expect different browsers to behave differently – Write and test your programs a little bit at a time • Browsers aren’t designed to report errors – Don’t expect to get any helpful error messages Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved