JavaScript Basics Introductory article (i)

Source: Internet
Author: User
Tags regular expression variable scope

One, data type and value

JavaScript: Allows the use of 3 basic types of data--------numbers, strings, Boolean values, and also supports two small data types null (empty) and undefine (undefined).
JavaScript: also supports conforming data types-Objects (object), two types of objects in JavaScript, an unordered set of named values represented by an object, and an ordered collection of numbered values. In fact, an ordered set is an array.
JavaScript: Also defines another special object----function, and some JavaScript-defined private objects (equivalent to the same concept as C # encapsulated classes, directly to the line)
<script>
Integer Direct: 3 or 10000, that's the number.
Floating-point direct quantity: 3.14,2345.567 and so on, just with a decimal point.
String literals: "3.14", "demo", etc., called strings, is a sequence of Unicode characters enclosed in single or double quotes.

Converts a number to a string: 1,var s = 100; s+= "What you name"; The numbers will go first.
Convert to String
2,var s = 100 + ""; plus an empty string
3. To make a number more visible to a string, you can use the string () function or
The ToString function is used by the.
Converts a string to a number: var product = "2" * "2"; When a string is used in a digital loop
It is automatically converted to a number, or it can be achieved by minus 0 or by using
Number () function
Boolean value:
Here I would like to share with you the content or conversion: In the future will be more. 1. When a Boolean value is used in a digital environment, true converts to number 1, and false to number 0, in a string environment, true is converted to string true, and false is converted to string false
Function:
A function is an executable snippet of JavaScript code. Here's the thing: the function, as a data type, can also be assigned to an object's properties like any other type, and when the assignment succeeds, the attribute is often used as a reference to which method. Used in the back.
Function Direct quantity: var square = function (x) {return x*x};//commonly used after, to be able to understand or remember
</script>


Javascript: Two, objects
1. Object

<script>

The code is as follows Copy Code
var o = new Object ()//Everyone note that JavaScript is sensitive to case sensitivity!
var now = new Date ()
var regex = new RegExp ("^+?d{1}d{3}$")//Regular expression
The direct amount of the object:
var point = {x:12,y:34};
var Point2 = {"Super": {day:sunday,day1:monday}}//object's properties refer to another object.

Conversion of objects:
When a Non-empty object is used in a Boolean environment: it is converted to true, and when used in a string environment, JavaScript invokes the object's ToString () method and uses the value returned by that function when used in a digital environment: JavaScript calls the object's valueof () method, if it returns a base type, the value is used and most of the time returns the object itself, in which case the JavaScript callback converts the object to a string using the ToString () method and then attempts to convert it to a number. The above concept, I hope you also understand that will be used in the future.
</script>

2, array

The code is as follows Copy Code


<script>
var array = new Array ();
var arr = new Array (1.2, "Javascript", {x:12,y:23})///with parameters
Array Direct Quantity:
var a = [1.2, Javascript, {x:12,y:23}]//array is [] number, object is {}, well remember!
</script>


3, null (empty)

The keyword null for JavaScript is a special value that represents no value, and null is often treated as a special value of an object type that represents a value that is not an object, when the value of a variable

NULL, it means that its value is not valid (Array,object, numeric, String, Boolean), details: null is converted to false in a Boolean environment;

The environment is converted into 0.

4, Undefined (not defined)

When you use a variable that is not declared, or when you use a variable that has already been declared but not assigned, or if you use an object property that does not exist, it returns the

is the undefined value, in the future (namespace, the module is still quite a lot, we want to understand), details: underfined in the Boolean environment it converted to false, in the digital environment

It will be converted to Nan. There is a difference between this and null. The object that encapsulates it is error.


Basic articles
javascript: Declaration of variables
Here are several ways to declare variables var value; var value,value1,value2;//declares multiple variables at the same time, but the values of these variables are undefined var i = 0,j = 0,k=100;//variable declaration, initialized in one. If you try to read a nonexistent variable (value) will be an error! But trying to assign values to a variable that does not use the Var declaration, JavaScript//implicitly declares the amount of change, and the declared variable is global. Details: So you create variables all try to use the var//variable scope (This problem is also easy to get out, we have to understand)

javascript: Scope of variables
These are the details, and I like the beginner must pay attention to avoid!

The code is as follows Copy Code
var Golbal = "Golbal"; Global variables
var local = ' local ';
function Area ()
{
Local variables have higher precedence than global variables
var local = "Arealocal"
JavaScript hides global variables when the variable name declared in the function body is the same as the global variable name
var Golbal = "Areagolbal";

document.write ("Local is:" +local + "and Golbal is:" + Golbal + "<br/>");
}

Area ();

Output: local Is:arealocaland golbal Is:areagolbal


What happens when you define a local variable inside a nested function? Look below:

  code is as follows copy code

var hope = " Moremoney ";
Function Createmore ()
{
var hope = ' Have more money '//local
Function Createmoreto ()//nested function
{
var hop E = "have more";//local
document.write ("Createmoreto hope is:" +hope + "<br/>");
 /output: Crea Temoreto Hope Is:have more money to much
}
 createmoreto ();//Call
 document.write ("Createmore Hope is : "+hope + <br/>");
//output: Createmore hope is:have more money
}
 createmore ();//Call


JavaScript: Pass value and address
  Here is also a more important concept! Don't miss it.
Value-Transfer                                   Address
Replication       actual copied values, there are different,         replicated only references to numbers. If you use this
         standalone copy.                      The new reference modifies the value, which changes to the original
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NB                         Sp References are also visible.

A separate copy of a value passed to a function is passed to a function by a reference to a value, if the function
The change to it has no effect outside of the function that modifies the value by passing the reference to it, which
Change is also visible.

Compare these two opposing values, usually by comparing the two references to determine whether they refer to
Byte comparison to determine whether equality is the same value.

javascript: base type and reference type
The basic rule of JavaScript is that the base type operates by passing values, and the reference type is manipulated by the address. (What's the value type, or what's the reference to see my last article)


Pass by value

The code is as follows Copy Code

var value = 1;
var copyvalue = value; Assign value to another variable
function AddTotal (TOTAL,ARG)
{
total+= Arg; Total = total + arg effect equals
}
Call the function, pass two arguments (you may think that this function changes the value of the global variable, in fact, the function is also the opposite copy)
AddTotal (Value,copyvalue);
if (value = = 1) copyvalue = 2;
document.write ("Total T" + value + "and Copyvalue tt" + copyvalue+ "<br/>");
Final output: Total 1and copyvalue 2

Delivered by address

  code is as follows copy code
 var Array = new Array ("JAVASCCCP");
 var objarray = array;
 function Modifyarray (arr)
 {
 arr[0] = "JAVASCRIPT";
 }
 //not calling function before
& Nbsp;document.write (Array[0] + "<br/>");
 //output JAVASCCCP;
 //after calling the function
 modifyarray (array);
 document.write (array[0]+ "<br/>");
 // Output uppercase JavaScript
 //by modifying Objarray will be the same effect
 objarray[0] = "Frank";
 document.write (array[0]+) <br/> ");
 /output Frank;
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.