Basic javascript programming handler variables, objects, data types and functions, javascript

Source: Internet
Author: User

Basic javascript programming handler variables, objects, data types and functions, javascript

In web standards, a webpage consists of three parts: structure, representation, and behavior.

Structure standard ----> XHTML;

Standard Format -----> CSS;

Behavior standard -----> javascript;

Javascript is a programming language specifically designed to add interactivity to web pages. It was initially developed by Netscape and finally submitted to the European Computer Manufacturers Association (ECMA ).

I. javascript features:

1. javascript is an interpreted language, which means that javascript code can be executed without prior compilation.

2. javascript cannot run independently from the browser

3. javascript does not allow users to read and write files (except for cookies). It adopts the same-origin policy and only allows interaction from the same domain.

Ii. What can javascript do?

1. provide programmers with the ability to fully control all elements in HTML webpages;

2. Dynamic text can be added to HTML webpages;

3. Respond to events generated by users in using webpages

4. Verify the input data

5. Check the visitor's browser

6. Create a cookie

Iii. How to add javascript to the XHTML webpage

1. embedded

2. Outreach

3. Event Introduction

Event introduction, simple example:

<!doctype html>
Iv. Variables

Javascript is weakly typed. explicit data type declarations are not required when defining variables, and the variables must be initialized.

var test1 = 'hi',test2='hi';var sex = 'male',age=12;var test;
  V. Data Types

Fractional font and non-numeric type

Non-numeric type: undefined, null, NaN, boolean, string

Number Type: number

1. Type features:

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

var temp;
The temp type is undefined, and the value is also undefined;

(2) When the variable is not declared and the latter function has no return value, it will be reflected as undefined;

(3) An error will be reported when an undeclared variable is used;

(4) typeof does not distinguish unassigned variables from undeclared variables;

Var temp1; alert (typeof temp1); // undefinedalert (typeof temp2); // undefinedalert (temp2 = undefined); // Error

(5) undefined is derived from null, so javascript defines them as equal;

alert(null==undefined);//true

(6) NaN cannot be used in arithmetic computation and is not equal to itself
2. Data Type Conversion

All data in javascript is actually a pseudo object, which means they actually have attributes and methods.

(1) convert boolean and numeric values to the toString () method ().

var bFound = false;alert(bFound.toString());//'false'var iNum = 10;var gNum = 10.0;alert(iNum.toString());//'10'alert(gNum.toString());//'10'
(2) For non-numeric raw values, javascript provides the parseInt () and parseFloat () Methods to convert to numbers.

These methods can be correctly run only when the string type that contains numbers is called. NaN is returned for other types or 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

You can use forced conversion to access a value of a specific type, even if it is another type.

Boolean (value );

Number (value );

String (value );

If the value to be converted is a string with at least one character, a non-zero number, or an object, the Boolean () function returns true. If the value is a null String, number 0, undefined, or null, it returns false. You can use the following code snippet to test the forced type conversion of the Boolean type.

Boolean('');//falseBoolean('hi');//trueBoolean(100);/trueBoolean(null);//falseBoolean(0);//falseBoolean(new Object());/true
The forced type conversion of Number () is similar to that of parseInt () and parseFloat (), except that it converts the entire value instead of the partial value. For example:

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 ).

If the string value can be fully converted, Number () determines whether to call parseInt () or parseFloat.

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 any value into a String. To perform this type conversion, you only need to call the toString () method that is passed as a parameter.

String(null);//'null'String(1);//'1'String(false);//'fasle'String(undefined);//'undefined'
3. Reference Data Type

Native objects: objects provided by javascript that are independent of the host environment, simply put, are objects defined in ECMA-262 standards. It includes Object, Function, Boolean, Date, Number, Error ....

Built-in object: the built-in object is actually a kind of native object, but unlike the native object, the built-in object does not need to be explicitly initialized because it has already been 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
(1) object class

The object class has the following attributes:

Constructor ----- reference to the function used to create an object. For the Object class, this reference points to the native object () function;

Prototype ----- reference the object prototype of the object. For all classes, it returns an instance of the Object class by default.

The following methods are available for Object:

HasOwnProperty (property) ------ determines whether an object has a specific property. This attribute must be specified with a string.

IsPrototypeOf (boject) ------ determine whether the object is a prototype of another object.

PropertyIsEnumerable (property) ------ determines whether a given property can be enumerated using the for... in statement.

ToString () ------ return the original string representation of the object.

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 of the original javascript data types Boolean, Number, and 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 class

The Global class is the most special object in javascript and does not need to be declared or initialized.

The eval () method is the most powerful method in javascript. It is like the entire javascript interpreter. It accepts a parameter, that is, the javascript string to be executed;

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

When using the typeof operator to store values of the reference type, a problem occurs. No matter what type of object is referenced, it returns "object ". Unlike typeof, The instanceof method requires developers to explicitly confirm that the object is of a specific type.

var o = new String('hello world');alert(o instanceof String);//true
Vi. Functions

If a function does not return a specific value or a return statement without parameters is called, the return value is undefined.








What is the basic data type of javascript?

Javascript is a weak data type and supports automatic conversion of data types. var is used to define either an int or a String.
There is no definition method like int I and String s like java. There is only one var I and var s. a Dom object can also be defined using var ~~

What method does Javascript use to obtain the data type of a variable?

Return a data type using typeof (var)
Possible values returned include
'Undefined'-if the variable is of the undefined type
'Boolean'-if the variable is of the boolean Type
'Number'-if the variable is of the number type
'String'-if the variable is of the string type
'Object'-if the variable is of the reference type or Null type

If (type of (123) = 'number') alert ('this is a number ')
And so on

Related Article

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.