Basic JavaScript (1)

Source: Internet
Author: User
Tags define local
This article briefly introduces the basic knowledge about getting started with javascript, including the scope transfer value and basic type and reference type of declared variables with variables. -- 1. Data Type and value javascript: three basic types of data can be used: Numbers, strings, Boolean... SyntaxHighlighter. all ();

This article briefly introduces the basic knowledge about getting started with javascript, including the scope transfer value and basic type and reference type of declared variables with variables.
-
-
I. Data Type and Value

Javascript: three basic types of data are allowed: Numbers, strings, and boolean values. In addition, two types of small data are supported: null and undefine ).
Javascript: it also supports conforming data types-objects. Objects in javascript are divided into two types. One is an unordered set of named values represented by an object, an ordered set of numbered values. In fact, an ordered set is an Array ).
Javascript: it also defines another special object-function, and some specialized objects defined by javascript (similar to the class encapsulated by C)
Script
Integer value: 3 or 10000. To put it bluntly, it is a number.
Float-Type Direct Volume: 3.14, 2345.567, etc., that is, with a decimal point
Number of strings: "3.14", "demo", and so on. A string is a sequence of Unicode characters enclosed by single or double quotation marks.
 
Convert the number to a string: 1, var s = 100; s + = "What you name"; the number is first
Convert to string
2, var s = 100 + ""; add an empty string
3. You can use the String () function or
Use the toString function.
Convert a string to a number: var product = "2" * "2"; in fact, when a string is used for a digital Ring
It is automatically converted into a number, or it can achieve the same effect by reducing the value of 0, or use
Number () function
Boolean value:
Here, I want to share with you the following content: there will be more use in the future. 1. When a Boolean value is used in a numeric environment, true is converted to 1, and false is converted to 0. In a string environment, true is converted to true, if the value is false, the string is converted to false.
Function:
A function is an executable JavaScript code segment. Here, the function can be used as a data type or as a property assigned to an object like other types. When a value is assigned successfully, the attribute is often used as a reference to a method. Commonly used.
Direct function quantity: var square = function (x) {return x * x}; // It is often used later. It must be understood or remembered.
Script


Javascript: 2. Objects
1. Object

Script

Var o = new Object (); // javascript is case sensitive!
Var now = new Date ()
Var regex = new RegExp ("^ +? D {1} d {3} $ ") // Regular Expression
Direct quantity of objects:
Var point = {x: 12, y: 34 };
Var point2 = {"super": {day: Sunday, day1: Monday }}// the property of the object references another object.
 

Object conversion:
When a non-empty object is used in a Boolean environment: it is converted to true. When used in a string environment, javascript calls the toString () method of the object and uses the value returned by the function, when used in a digital environment: javascript calls the valueOf () method of the object. If a basic type is returned, this value is used. In most cases, the returned value is the object itself, in this case, javascript calls the toString () method back to convert the object into a string, and then tries to convert it to a number. I hope you will understand the above concepts and will use them later.
Script

2. Array

Script
Var array = new Array ();
Var arr = new Array (1.2, "Javascript", {x: 12, y: 23 }) //
Direct array quantity:
Var a = [1.2, "Javascript", {x: 12, y: 23}] // The array is the [] number, and the object is the {} number. It is easy to remember!
Script
 


3. Null (Null)

The javascript keyword Null is a special value, which indicates that there is no value. null is often seen as a special value of the object type, that is, representing the value of no object. When the value of a Variable

If it is null, it indicates that its value is not valid (Array, Object, number, String, Boolean value). Details: null is converted to false in a Boolean environment;

Change the environment to 0.

4. Undefined (Undefined)

When a declared variable is used but no value is assigned, or a non-existing object attribute is used

That is, the undefined value. In the future (namespace, the module is still used quite a lot, you need to understand), details: underfined converts it to false in a Boolean environment, in a digital environment

It is converted to NaN. This is different from null. The object that encapsulates it is Error.


Basics
Javascript: Variable Declaration
The following are several ways to declare variables: var value; var value, value1, value2; // multiple variables are declared at the same time, but the values of these variables are undefined var I = 0, j = 0, k = 100; // variable declaration, initialization integration. // If you try to read a nonexistent variable (value), an error is returned! However, if you try to assign a value to a variable that is not declared using Var, javascript // implicitly declares the amount of changes, and the declared variables are still global. Details: Therefore, we try to use the scope of Var // variables when creating variables (this problem is also easy to understand)

Javascript: variable scope
These are all details, so be sure to avoid them if you are a beginner like me!
 

The Code is as follows:
Var golbal = "golbal"; // global variable
Var local = "local ";
Function area ()
{
// The priority of local variables is higher than that of global variables.
Var local = "arealocal"
// When the variable name declared in the function body is the same as the global variable name, javascript hides the global variable.
Var golbal = "http://www.hzhuti.com/nokia/n97 ";

Document. write ("local is:" + local + "and golbal is:" + golbal +"
");
}

Area ();
 

// Output: local is: arealocaland golbal is: http://www.hzhuti.com/nokia/n97/


How can I define local variables in nested functions? See the following:

The Code is as follows:
Var hope = "moremoney ";
Function createmore ()
{
Var hope = "have more money"; // partial
Function createmoreto () // nested function
{
Var hope = "have more money to much"; // partial
Document. write ("Createmoreto hope is:" + hope +"
");
// Output: Createmoreto hope is: have more money to much
}
Createmoreto (); // call
Document. write ("Createmore hope is:" + hope +"
");
// Output: Createmore hope is: have more money
}
Createmore (); // call
 


Javascript: transfer value and address
This is also an important concept! Do not miss it.
Value Transfer address
Copy the actually copied value. Different values are copied only by referencing numbers. If you use this
Independent copy. The new reference modifies the value.
References are also visible.

Passed to the function is an independent copy of the value passed to the function is a reference to the value, if the function
The change to it does not affect the modification of the value by passing the reference to it.
Changes are also visible.

Compare the two opposite values. Generally, two references are compared one by one to determine whether the referenced values are
Byte comparison to determine whether the value is equal to the same value.

 

Javascript: basic type and reference type
The basic rule of javascript is: the basic type is operated by passing values, and the reference type is operated by passing addresses. (For details about the value type or reference, refer to my previous article)

 


Pass by value
 

Var value = 1;
Var copyvalue = value; // assign value to another variable
Function addTotal (total, arg)
{
Total + = arg; // total = total + arg equivalent
}
// Call the function and pass two parameters (You may think that this function has changed the value of the global variable. In fact, this function is not used, and the function is also opposite copy)
AddTotal (value, copyvalue );
If (value = 1) copyvalue = 2;
Document. write ("total t" + value + "and copyvalue tt" + copyvalue +"
");
// Final output: total 1and copyvalue 2

 
 

Pass by address


Var array = new Array ("Javascccp ");
Var objarray = array;
Function modifyArray (arr)
{
Arr [0] = "JAVASCRIPT ";
}
// Before the function is called
Document. write (array [0] +"
");
// Output Javascccp;
// After the function is called
ModifyArray (array );
Document. write (array [0] +"
");
// Output uppercase JAVASCRIPT
// Modifying objarray will have the same effect
Objarray [0] = "Frank ";
Document. write (array [0] +"
");
// Output Frank;

 
From php development
 

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.