What is JavaScript? What is a JavaScript error VK


Language Java programming Script is an object-oriented scripting language originally developed by Netscape Communications under the name LiveScript, but then renamed "JavaScript" and, given its syntax, is closer to Sun Microsystems' Java. JavaScript was later standardized by ECMA under the name ECMAScript. Microsoft calls its versions JScript.

The change in name from LiveScript to JavaScript occurred around the same time that Netscape included support for Java technologies in the Netscape Navigator browser. This change has created a lot of confusion in the minds of those learning to program for beginners. There is no real connection between Java and JavaScript, their similarities begin and end with similar syntax and the fact that both languages ​​are widely used on the Internet.

JavaScript is an object-oriented scripting language that communicates through an interface called Document Object Model(DOM) with content, can be executed on the server side (web servers) and on the client side in the user's web browser when viewing web pages. Many websites use JavaScript technologies client-side for building powerful dynamic web applications in programming for dummies. It can use Unicode and can use the power and strength of regular expressions (this was introduced in version 1.2 of Netscape Navigator 4 and Internet Explorer 4). JavaScript expressions contained as a string can be executed using the EVAL function.

One of the main tasks for JavaScript is small functions embedded in HTML pages that allow you to interact with the DOM from the browser to perform certain tasks that are not possible in static HTML: such as opening a new window, validating values ​​entered into a form, changing the image on hover. mice, etc. Unfortunately, creating such functions is quite tedious because browsers are not standardized, different browsers may create different scripting objects or methods, and so you often have to write various options JavaScript functions for various browsers, but this is not very convenient when learning the basics of programming.

JavaScript / ECMAScript is supported by such engines as:

  • Rhino
  • SpiderMonkey

Environment

The markup comment is required to ensure that the code does not display as text in browsers that do not recognize the . tags in XHTM/XML documents, however, will not work if commented out. Modern browsers, which support XHTML and XML are well designed enough to recognize , so in these documents the code remains uncommented.

An HTML element can generate internal events to which a script handler can be connected. To create a valid HTML 4.01 document, you must insert the appropriate default script link statement in the document head section.

Elements of language

Variables

Variables are usually dynamically typed. Variables are defined either by simply assigning a value to them or by using the "var" operator. Variables declared outside a function are in "global" scope, visible throughout the web page; variables declared inside a function are local to that function. To pass variables from one page to another, the developer can set a "cookie" or use a hidden frame or window in background for their storage.

Data structures

The main type is a data structure in the form associative array similar to hashes in the language Perl programming or Python, Postscript and Smalltalk dictionaries.

Elements can be accessed by numbers or associated names (if these have been defined). Thus, the following expressions may all be equivalent:

MyArray,
myArray.north,
myArray["north"].

Declaring Arrays

MyArray = new Array(365);

Arrays are implemented so that only certain (non-empty) elements will use memory, they "discharge the arrays". If we set the set myArray = "something there" and myArray = "something else there", then we have used space only for these two elements.

Objects

JavaScript has several kinds of built-in objects, namely Object, Array, String, Number, Boolean, Function, Date and Math. Other objects belong to DOM objects (windows, forms, links, etc.).

By defining constructor functions, you can define objects. JavaScript is a prototype-based object-oriented language. You can add additional properties and methods to individual objects after they were created. To do this, you can use a prototype statement for all instances of a particular type of object.

Example: Creating an Object
// Constructor function

Function MyObject(attributeA, attributeB) ( this.attributeA = attributeA this.attributeB = attributeB )
// Create an object
obj = new MyObject("red", 1000)

// Access an object attribute
alert(obj.attributeA)

// Access attribute with associative array designation
alert(obj["attributeA"])

The hierarchy of objects can be reproduced in JavaScript. For example:

Function Base() ( this.Override = _Override; this.BaseFunction = _BaseFunction; function _Override() ( alert("Base::Override()"); ) function _BaseFunction() ( alert("Base::BaseFunction()" ); ) ) function Derive() ( this.Override = _Override; function _Override() ( alert("Derive::Override()"); ) ) Derive.prototype = new Base(); d = new Derive(); d.Override(); d.BaseFunction();

As a result, we get on the screen: Derive::Override() Base::BaseFunction()

Control instructions
If ... else if (condition) ( statements )
Cycles
while (condition) ( statements ) Do ... while do ( statements ) while (condition); For loop for (; ; ) ( statements ) For loop ... in
This loop goes through all the properties of an object (or element in an array)
for (variable in object) ( statement )

Selection operator
switch (expression) ( case label1: statements; break; case label2: statements; break; default: statements; )

Functions
The body of the function is contained in (the body can be empty), and the list of arguments is indicated inside () following the function name. Functions can return a value after execution.

Function(arg1, arg2, arg3) ( statements; return expression; )

As an example, let's look at a function based on the Euclidean greatest common divisor algorithm:

Function gcd(a, b) ( while (a != b) ( if (a > b) ( a = a - b; ) else ( b = b - a; ) ) return a; )

The number of arguments when calling a given function does not necessarily have to provide as many arguments as were specified when the function was declared. In a function, arguments can also be accessed through an argument array.

Each function is an instance of a function, basic type object. Functions can be created and assigned like any other objects:

Var myFunc1 = new Function("alert("Hello")"); var myFunc2 = myFunc1; myFunc2();

Result on screen:

User interaction

Most user interaction is done using HTML forms, which can be accessed through the HTML DOM. However, there are also some very simple remedies communication with the user:

Alert dialog box
Confirm dialog box
Dialog lines
Status bar
Consoles

Text elements can be the source of various events, which can trigger actions if an EMCAScript event handler is registered. In HTML, these event handler functions are often defined as anonymous functions directly in the HTML tags.

JavaScript is a programming language that allows you to create scripts that are embedded in HTML pages and executed in the browser of the page visitor.

Modern browsers are required to support JavaScript.

JavaScript commands are added to web pages using the tag, and the executable script must be entered in the Text window CMS WordPress. There can be as many containers in one document as you like. The “type=’text/javascript’” attribute is optional, since the default is javascript.

Here's an example:


document. write("Output text standard command JavaScript." );

The tag's type attribute tells the browser which commands to use. scripting language are embedded further before the closing tag.

When mastering the JavaScript language, you first need to master comments, which should be used especially often when you first start using the language.

JavaScript allows short comments—comments that are no longer than the length of the line. Anything after two characters // to the end of the line will be a short comment. Here are two examples of short comments:

// 1. The command below displays the Paragraph in bold
document. write(");

document. write( Hello, World!); // 2. Output in italics the string Hello, World!

Additionally, JavaScript allows multiline comments—comments that span multiple lines. Here is an example of such a comment:

/*
The first command displays the paragraph in bold,
and the second command displays the paragraph in italics
*/
document. write("

Paragraph displayed in bold font.

" ); document. write("

Paragraph written in italics.

» );

It is impossible to describe all the features of the JavaScript language on one page; on it I will write down only the most significant, in my opinion, features of the language.

The script of this language can either be embedded directly in the HTML code of the page, or placed in a file that can be called different pages. Here is an example of calling a file with a script:

If specified src attribute="… ", then the contents of the tag are ignored, that is, script cannot be included simultaneously in one tag external script and specify the code, so you need to choose: the script either contains src or contains code. If necessary, we simply add the code to another script.

By the way, WordPress versions 4.0 does not recognize JavaScript code in the HTML code of pages and spoils it by hiding JavaScript code behind







2024 gtavrl.ru.