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

Source: Internet
Author: User

In the Web Standard. The Web page consists of three parts: structure, manifestation 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 Corporation. Finally submitted to the European Association of Computer Manufacturers (ECMA).

First, JavaScript features:

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

2. JavaScript cannot be executed independently from the browser

3, JavaScript does not agree to read and write the user's files (except cookies), using the same-origin strategy. Just agree to interact from the same domain.

Ii. what JavaScript can do

1, to the program ape a full control of the HTML page all elements of the ability;

2, 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. Check the browser of the person who interviewed him

6. Create a cookie

Iii. How JavaScript is added to the XHTML Web page

1. Embedded

2, the Outreach

3. Event Introduction

Event ingestion. Simple Demo Sample:

<!doctype html>Iv. Variables

JavaScript is a weak type. A data type declaration that does not need to be understood when defining a variable, and the variable 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 also a type, and the value of such a type is undefined, such as

var temp;
The type of temp is undefined. The value is also undefined;

(2), when the variable does not declare 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's 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 bfound = False;alert (bfound.tostring ()),//' false ' var iNum = 10;var Gnum = 10.0;alert (inum.tostring ());//' Alert ( Gnum.tostring ());//' 10 '
(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 include numbers, and they are capable of being executed correctly; The return is Nan for other types or for pure character types.

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

Use casts to access specific types of values. Even if it is still of a 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. Assuming that the value is an empty string, the number 0, undefined, or null, it returns FALSE. Ability to test a Boolean type cast with the following code snippet.

Boolean (");//falseboolean (' Hi ');//trueboolean (+);/trueboolean (null);//falseboolean (0);//falseboolean (new Object ());/true
The coercion type conversion of number () is similar to the parseint () and parsefloat () methods, just that it converts the entire value. Instead of a partial value.

Like what:

parseint (' 4.2.6 ');//4parsefloat (' 4.2.6 ');//4.2number (' 4.2.6 ');//nan
Because the entire string value cannot be converted to a number, it is not a number (NaN).

Assuming that the value of the string can be fully converted, number () infers whether to call parseint () or call the Parsefloat () method.

Number (false);//0number (true);//1number (undefined);//nannumber (null);//0number (' 2.5 ');//2.5number (' 56 ');// 56Number (' 2.2.7 ');//nannumber (new Object ());//nannumber (100);//100
String () is the simplest because it can convert whatever value to a string. To run such a coercion type conversion, you simply need to invoke 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: An object that is provided by JavaScript, independent of the hosting environment, in simple words. Is the object defined in the ECMA-262 standard.

It contains object, Function, Boolean, Date, number, Error ....

Built-in objects: Built-in objects are actually one of the native objects. But unlike the native object. The built-in object does not need to be explicitly initialized because it is 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------Infer whether an object has a specific property. This property must be specified with a string.

isPrototypeOf (boject)------Infer whether the object is a prototype of another object.

propertyisenumerable------Infers whether a given property can be enumerated with for...in statements.

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 each are the wrapper classes of the JavaScript raw data type, Boolean, number, string.

(3) Array class

var colors = new Array (' Red ', ' blue ', ' yellow '); colors[25]= ' black '; alert (colors.length);//26
The value of 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 is like an interpreter for the entire JavaScript. Accepts a parameter, which is the JavaScript string to run;

eval (' Hello '), equivalent to alert (' Hello ');//hello;var msg = ' Hello World '; eval (' Alert (msg) ');//msgeval ("function go () { ' Say hi '); go ();//say hi
(5) instanceof operator

When using the TypeOf operator to store a value with a reference type, there is a problem, regardless of what type of object is referenced, and it returns "Object". Unlike typeof, the Instanceof method requires the developer to clearly confirm that the object is of a particular type.

var o = new string (' Hello World '); alert (o instanceof string);//true
Vi. Functions

Assume that the function has no clear return value. or call a return statement with no parameters, 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.