JavaScript Scripting Language, Concept Basics - Designing...

JavaScript scripting language

The scripting languages ​​allow you to include in the web pages the program code that will set the behavior of the web page in the browser when users are working with them. There are different scripting languages, but the most common language is JavaScript. JavaScript is an interpreted programming language standardized by the international organization ECMA in the ECMA-262 specification. Languages ​​JavaScript, Jscript and ActionScript are an extension of the ECMA-262 standard.

There are 3 options for embedding JavaScript in web pages:

1. Location within the page. You can use the & lt; script & gt; & lt;/script & gt; tags to add the JavaScript code to the page. For example:

& lt; script type = text/javascript & gt; alert ('Hello, World!'); & lt;/script & gt;

2. Location within the tag. The HTML specification describes the set of attributes used to specify the event handlers. Example usage:

& lt; a href = 'delete.php onclick- return confirm ( Are you sure?); & gt; Delete & lt;/a & gt;

3. Putting it in a separate file. You can save the script in a separate file, and then connect it using the

& lt; script type = text/javascript src = URL_of_cueHapMeM & gt; & lt;/script & gt;

Concept Basics

Using JavaScript, scripts, or scripts are described which can be executed using special modules - script interpreters , included in all modern web browsers.

The basic syntax of the JavaScript language is much like the syntax of languages ​​such as C ++, Java, and C #. The script code is written in text format and consists of statements (statements), blocks, comments and functions. and data, such as strings, numbers , and expressions. To indicate the end of an operator , the & quot ;; (semicolon). Block is a set of JavaScript statements enclosed in curly braces {}.

Variables and data types. JavaScript does not support strict type control, so variables do not need to declare a type explicitly. They get a type that is equivalent to the type of values ​​assigned to them.

You can use var to declare variables. The var statement is mandatory when declaring local variables within a function. You can use variables without declaring them using the var statement. However, undeclared variables are not allowed to be used in expressions. The JavaScript language distinguishes between large and uppercase characters in variable names, that is, the names Name and name are considered different.

In the JavaScript language, six types of data are used - numbers (Number), strings (String), objects (Object), logical (Boolean), null, and undefined:

JavaScript supports numbers, as integers, and floating point. There are also special representations of numbers, for example NaN (not a number). Examples of numbers are: 3.14 (real number), 15 (integer), 0177 (octal number 177), 0XA8 (hexadecimal number A8).

Strings are declared using double quotes or apostrophes. A string can consist of several Unicode characters.

The

Boolean type accepts both true and false. Any expression equal to 0 is considered equivalent to false, and any expression equal to a number other than 0 will be equivalent to true.

A null variable has no specific value. Undefined means that the type of ns is defined. The value undefined has a variable after its declaration and before assigning it to a certain value.

In many cases, JavaScript performs data type conversions automatically when they are needed. For example, if you add a row and a number, the number will be converted to a string.

However, you can also specify an explicit conversion of the value of a variable to the required type. For example, numbers can be converted to strings, and strings - into numeric type (parselnt () and parseFloat ()).

Expressions . In JavaScript, logical or numeric expressions are selected. Any permissible combination of constants and variables, united by signs of operations, is an expression.

The equal sign (=) is used as an assignment. For example, the expression Pi = 3.14; means Assign 3.14 to the variable Pi & quot ;. And to set the operation of comparing two values ​​for equality, a double equal sign (=) is used.

The + in the expression means addition or string concatenation .

Operators . The language supports conditional statements if and if ... else. To combine several conditions, you can use the logical operations || (OR) or & amp; amp; (AND).

In JavaScript, there are several types of loop operators: for, for.,. in, while, do ... while and a switch statement. Also, there is a break statement for the break loop. The continue statement is used to immediately jump to the next iteration, skipping the rest of the execution of the current iteration code.

Functions . In JavaScript, the function acts as one of the main data types. There are two kinds of functions', built-in and defined. The programmer has the ability to create custom functions. The function definition consists of declaration of parameters and of the statement block JavaScript. Before using the function, the ec has to be defined beforehand. The function description has the following form: function name (arguments) {statements}

Here name is the identifier that specifies the name of the function, the arguments are an optional comma-separated list of identifiers, which contains the names of the formal arguments of the function, and the operators are any set of operators, called the body function and is executed when it is called. For example: function area (radius) {return pi * radius * radius;}

This function has the name area and one formal argument radius. When you call this function, instead of a formal argument, its actual value is substituted, the function calculates the area of ​​the circle based on this value and returns the resulting number with the return operator.

The variables declared in the body of the function are local, ie they are only available in her body.

When a function is called in JavaScript, the following arguments are passed:

• Arguments of primitive types are passed to by value.

• Objects (both built-in and user-defined) are passed by reference. This means that all changes to the object's properties in the body of the function are performed directly in the object itself, and not in its local copy and, hence , are retained after the function returns.

Objects are the main data type in JavaScript. Objects are a collection of properties and methods. You can use the Object type to declare variables. The following object types are used in JavaScript scripts:

Embedded objects representing different types of data, properties, methods inherent in the JavaScript language itself, regardless of the contents of the HTML page, for example. Array, String, Date, Number, Function, Boolean, Math.

The objects of the document elements, are included in the DOM model, that is, corresponding to the content and behavior of the web page processed by the browser. They are created by the browser when the HTML page is parsed (parsed). Examples: window, document, location, navigator, etc.

User objects that are created by the programmer in the process of creating a script using constructors of the type of objects (class).

Examples of embedded JavaScript objects are arrays. Arrays are objects of type Array. If, when declaring arrays, one argument is given (a non-negative integer), an empty array of the appropriate length is created. If you pass one argument that is not a number or more than one argument, then an array is created that is filled with these elements:

a = new Array (); // empty array

b = new Array (17); // array of length 17

c = new Array (10, '12345'); // an array of two elements: numbers and lines

Numbering of array elements starts from zero. In JavaScript, an array can consist of disparate elements. Elements of an array can also be arrays. To work with array elements, there are various properties (for example, length - the number of elements in a given array) and methods (for example, join (), reverse (), and sort ()).

To search through the elements of the array, use the for statement (variable in object). For example:

for (d in document)

document.write ("document." + d + "= & lt; b & gt;" + document [d] + "& lt;/b & gt; & lt; br>);

Also We Can Offer!

Ошибка в функции вывода объектов.