[ Pobierz całość w formacie PDF ]
.Teach Yourself JavaScript in 21 Days by Sams Publishing is an example of one of these books.SummaryJavaScript is a powerful, interpreted scripting language that is built-in to the Netscape Navigator software.JavaScript is full-featured and allows for the creation of dynamic pages, interactive content, forms input checking, and much more.JavaScript's syntax is based on Java's syntax, but otherwise remains as a different language with a different role.JavaScript excels in its ability to add client-side processing of forms, create HTML content on-the-fly, and tightly integrate HTML with Java.JavaScript requires only a text editor for the creation of applications, and because Netscape's Navigator has a built-in JavaScript interpreter, JavaScript applications may be run immediately after they have been typed in simply by loading them into the browser window.JavaScript enables you to embed your JavaScript code directly into an HTML document with the use of the <SCRIPT>…</SCRIPT> tags.By hiding the code from other browsers using HTML comment tags and JavaScript comment statements, you can try to ensure that your JavaScript page stays compatible with most browsers that do not support JavaScript.The next chapter delves into the JavaScript language itself, its syntax, and how you can use the language to create your own JavaScript-enabled Web pages.Chapter 42The JavaScript languageThe JavaScript language is not difficult to learn, but it does require knowledge of many of the syntax conventions of Java.An understanding of these conventions is necessary to adequately use JavaScript.It may be helpful to gain a thorough understanding of the Java syntax and conventions by reading some of the earlier chapters in this book before proceeding.JavaScript FundamentalsThis section covers the basic components of the JavaScript language.These components include variables and values, data types, literals, properties, arrays, and comment statements.As the basic building blocks of JavaScript, an understanding of these fundamentals is the first step toward the mastery of the JavaScript language.Variables and ValuesJavaScript variables are similar to variables in any other programming language; they hold values that you use in your application.JavaScript enables you to name your case-sensitive variables so you may reference them elsewhere in your code.You can name variables anything you wish.You may opt to use descriptive names such as number_of_times_clicked, or short, generic names such as foo.The only restrictions on variable names is that they must begin with a letter or underscore (_).JavaScript variables can contain the following different types of values:lnumbers, for example, 14 or 1.25lllogicals (Boolean), can be True or Falsellstrings, for example, Hello WorldlJavaScript also recognizes the null keyword for specifying null values for variables.JavaScript has reduced the confusion in deciding which data type to use for different types of numbers.JavaScript uses a single data type for numbers, which means that a JavaScript number can contain the equivalent of integers, reals, and doubles without the need for specialized types.In addition, JavaScript handles Date objects with this same data type, further simplifying code development under JavaScript.The var statement is used to declare variables in JavaScript.Each variable is given a name and optionally an initial value.Outside of a function, the var statement is optional, but it is highly recommended that you always use var to avoid the possibility of overwriting local variables with global variables.Local variables are generally declared within a function with the intention that only the function will be able to use the variable.Global variables are declared outside of any function, making the variable available to any function.Therefore, to avoid conflict, the var statement will ensure your variables safety in these situations.Following is the syntax for the var statement:var varname [= value] [., varname [= value] ]Multiple variables may be declared from one var statement.The following example demonstrates this:var num_cans = 10, price_per_can = 2.0;Data TypesJavaScript fully supports variables of different data types, but does so loosely to enable you, the programmer, to more quickly develop applications without the headache of strict type specification prior to execution.JavaScript applications can handle a great number of different types of data, but JavaScript manages to do this with only three distinct data types.In addition, JavaScript can decide for you what data type your variable should be during script execution.Converting between different types is very easy and straightforward.Data types are converted automatically when your JavaScript application runs.JavaScript follows one simple rule when converting types; it performs the conversion from left to right.In other words, the right-hand operand will be converted to the type of the left-hand operand.No matter how complex the conversion, JavaScript always follows this left-to-right approach.For example, if you had the following variables:lvar1 = “10”llvar2 = 20lThen performed the following statements:lx = var1 + var2lly = var2 + var1lThe first statement would convert var2 to a string, because the operand on the left, var1, is a string.The result would be that x would contain the string “1020”.In contrast, the second statement would convert var1 to a number, because the operand to its left, var2, is a number.The two numbers would then be added together to form the result of 30 in y.As you saw in the previous example, JavaScript can convert strings that contain numeric characters into numbers quite easily.If a string contains non-numeric values, such as “Paul”, and you try to set a number variable to contain that value, JavaScript will generate an error because it cannot convert “Paul” into a numeric value.LiteralsMany times when creating new variables, you need to specify an initial value.These types of fixed values are called literals.Literals are not variables, but rather, constant expressions of values for data types.For example, some literal values could include2472.745“programming is fun”When expressing literal values for integers, you may use three different bases.You may use decimal (base 10), hexadecimal (base 16), or octal (base 8) format.To specify a decimal literal, simply use a series of digits without any leading zeros.For example42 (a decimal literal)To specify an octal literal, precede the number with a leading 0 (zero).Octal literals can only include digits 0-7.For example010 (an octal literal)Finally, when specifying a hexadecimal literal, precede the number with a leading 0x (or 0X).Hexadecimal literals can include all digits (0 - 9) and the letters a-f and A-F [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • lunamigotliwa.htw.pl
  •