JavaScript BASIC programming variables, objects, data types, and functions

Source: Internet
Author: User

In Web standards, a Web page consists of three parts: structure, expression, and behavior.

Structural standard---->XHTML;

Presentation Standard----->CSS;

Behavioral Standards----->javascript;

JavaScript is a programming language specifically designed to add interactivity to Web pages, originally developed by Netscape and finally presented to the European Association of Computer Manufacturers (ECMA).

First, JavaScript features:

1. JavaScript is an interpreted language, which means that JavaScript code does not need to be pre-compiled to execute.

2. JavaScript cannot run independently from the browser

3, JavaScript does not allow read and write user's files (except cookies), using the same-origin policy, only allow interaction from the same domain.

Ii. what JavaScript can do

1. Give the programmer the ability to control all the elements in the HTML Web page in a comprehensive way;

2, you can put the dynamic text in the HTML page;

3. Responding to events generated by users in the use of Web pages

4. Verify the data used for input

5. Detecting the visitor's browser

6. Create a cookie

Iii. how JavaScript is added to XHTML Web pages

1. Embedded

2, the Outreach

3. Event Introduction

Event ingestion, simple example:

<!DOCTYPE HTML><HTMLLang= "en"> <Head>  <MetaCharSet= "UTF-8">  <Metaname= "Generator"content= "editplus®">  <Metaname= "Author"content="">  <Metaname= "Keywords"content="">  <Metaname= "Description"content="">  <title>Document</title> </Head> <Body>  <formAction="#"Method= "Get">    <inputtype= "button"value= "Press Me"onclick= "alert (' Hello World ');"/>  </form> </Body></HTML>

Iv. variables

JavaScript is weakly typed and does not require explicit data type declarations when defining variables, and variables must be initialized

var test1 = ' Hi ', test2= ' Hi '; var sex = ' male ', age=12; var test;

V. Types of data

Fractional font and non-digital type

Non-numeric: Undefined, null, NaN, Boolean, string

Digital Type: Number

1. Type characteristics:

(1), undefined is not an error, it is a type, and this type of value is undefined, such as

var temp;

The type of temp is undefined, and the value is undefined;

(2), when the variable does not declare that the latter function has no return value, it will be reflected as undefined;

(3), the use of an undeclared variable will be an error;

(4), typeof does not distinguish between unassigned variables and undeclared variables;

var Temp1;alert (typeof Temp1); // undefinedalert (typeof Temp2); // undefinedalert (temp2==undefined); // Error

(5), undefined are derived from null, so JavaScript defines them as equal;

Alert (null==undefined); // true

(6), Nan cannot be used for arithmetic calculations, and it is not equal to itself
2. Data type Conversion

All the data in JavaScript is actually a pseudo-object, which means that they actually have properties and methods.

(1), the simplest method of converting a Boolean value, a numeric value to a string, ToString ().

var false ; alert (bfound.tostring ()); // ' false ' var iNum = ten; var gnum = 10.0; alert (inum.tostring ()); // 'alert ' (gnum.tostring ()); // ' Ten '

(2) for non-numeric primitive values, JavaScript provides methods for converting the parseint () and parsefloat () methods to numbers.

These methods are called only for string types that contain numbers, and are returned as Nan for other types or for pure character types.

 parseint (' 1234blue '); // 1234  parseint (' 0xA '); // 10  parseint (' 22.5 '); // 22  parseint (' Blue '); // nan  parsefloat (' 1234blue '); // 1234.0  parsefloat (' 0xA '); // 0  parsefloat (' 22.5 '); // 22.5  parsefloat (' 22.345 '); // 22.345  parsefloat (' 0908 '); // 908  parsefloat (' Blue '); // nan  

(3) Forced conversion

A cast can be used to access a value of a particular type, even if it is of a different type.

Boolean (value);

Number (value);

String (value);

The Boolean () function returns True when the value to be converted is a string of at least one character, not a 0 number, or an object. If the value is an empty string, the number 0, undefined, or null, it returns FALSE. You can test a Boolean type cast with the following code snippet.

Boolean ("); // false Boolean (' Hi '); // true Boolean (+);/trueboolean (null); // false Boolean (0); // false Boolean (new Object ());/true

The coercion type conversion of number () is similar to the parseint () and parsefloat () methods, except that it transforms the entire value, not the partial value. For example:

parseint (' 4.2.6 '); // 4parsefloat (' 4.2.6 '); // 4.2 Number (' 4.2.6 '); // NaN

Because the entire string value cannot be converted to a number, it is not a number (NaN).

If the value of the string can be fully converted, number () will determine whether to call parseint () or call the Parsefloat () method.

 Number (false ); // 0  Number (true ); // 1  Number (undefined); // nan  Number (null ); // 0  Number (' 2.5 '); // 2.5  Number (' 56 '); // 56  Number (' 2.2.7 '); // nan  Number (new  Object ()); // nan  Number (100); // 100  

String () is the simplest because it can convert any value to a string. To perform this coercion type conversion, you only need to call the worthwhile ToString () method passed in as a parameter.

String (null); // ' null ' String (1); // ' 1 ' String (false); // ' Fasle ' String (undefined); // ' undefined '

3. Reference data type

Native object: The object that is provided by JavaScript, independent of the hosting environment, is simply the object defined in the ECMA-262 standard. It includes object, Function, Boolean, Date, number, Error ....

Built-in objects: Built-in objects are actually one of the native objects, but unlike native objects, built-in objects do not need to be explicitly initialized because they are already initialized. ECMA-262 only defines two built-in objects: globle and math;

Host object: A browser-related object. All BOM and Dom objects belong to the host object.

4. JavaScript class
(1) Object class

The object class has the following properties:

Constructor-----A reference to the function that created the object. For the object class, the reference points to the native object () function;

Prototype-----A reference to the object's prototype of the object. For all classes, it returns an instance of the object class by default.

Object also has the following methods:

hasOwnProperty------Determine whether an object has a specific property. This property must be specified with a string.

isPrototypeOf (boject)------Determine if the object is a prototype of another object.

propertyisenumerable------Determine whether a given property can be enumerated with a for...in statement.

The toString ()------Returns the original string representation of the object.

The valueOf ()------Returns the original value that best fits the object.

(2) Raw data type wrapper class

The Boolean, number, and string classes in JavaScript are the wrapper classes for the JavaScript raw data type Boolean, number, string, respectively.

(3) Array class

var New Array (' Red ', ' blue ', ' yellow '); colors[25]= ' black '; alert (colors.length); //  -

The value below is 3----24 is null.

(4) Global category

The global class is the most special object in JavaScript, and it does not need to be declared and initialized.

The eval () method is the most powerful method in JavaScript, which, like the entire JavaScript interpreter, accepts a parameter, the JavaScript string to execute;

Eval (alert (' Hello ')), equivalent to alert (' Hello '); // Hello; var msg = ' Hello world '; eval (' Alert (msg) '); // msgeval ("function go () {' Say hi '}"); Go (); // say hi

(5) instanceof operator

Storing a value with a reference type when using the TypeOf operator will cause a problem, regardless of what type of object is referenced, and it returns "Object". Unlike typeof, the Instanceof method requires the developer to explicitly confirm that the object is of a particular type.

var New String (' Hello World 'instanceof string); // true

Vi. functions

If the function has no explicit return value, or if a return statement with no arguments is called, then the value it really returns is undefined.






JavaScript BASIC programming variables, objects, data types, and functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.