JavaScript Basics (1)
Basics
Javascript: 1. Data types and values
javascript: allows the use of 3 basic types of data----numbers, Strings and Boolean values. In addition, it also supports two small data types, null (empty) and undefined (undefined).
Javascript: It also supports the data type-object (object). There are two types of objects in JavaScript, one An object represents an unordered collection of named values, and the other represents an ordered collection of numbered values. In fact, an ordered collection is an array (Array).
javascript: It also defines another special object - function, and some special objects defined by javascript (the same concept as the C# encapsulated class, just use it directly)
<script> <br>Integer direct quantity: 3 or 10000, to put it bluntly, it is a number <br>Floating point literals: 3.14, 2345.567, etc., which are the ones with a decimal point. <br>String literals: "3.14", "demo", etc., the so-called strings are Unicode enclosed in single or double quotes. character sequence. <br>Convert the number to a string: 1, var s = 100; s ="What you name"; The number will be <br> converted into a string first <br>2, var s = 100 ""; add one Empty string <br> 3. To convert the number into a string more clearly, you can use the String() function or <br> or use the toString function. <br>Convert a string into a number: var product = "2" * "2"; In fact, when a string is used in a numeric environment <br> it will automatically be converted into a number, or it can be subtracted by 0 The same effect can be achieved, or you can use the <br>Number() function <br>Boolean value: <br>What I want to share with you here is conversion: it will be used more in the future. 1. When a Boolean value is used in a numeric environment, true is converted to the number 1, and false is converted to the number 0. In a string environment, true is converted to the string true, and false is converted to the string false <br>Function: <br>A function is an executable JavaScript code segment. Let me talk about it here: As a data type, functions can also be assigned to the properties of objects like other types. When the assignment is successful, the properties are often regarded as references to which methods. Commonly used later. <br>Function direct quantity: var square = function(x){return x*x};//It will be used frequently later and must be understood or remembered<br></script>
Javascript: 2. Object
1. Object
< ;script>
var o = new Object();//Attention everyone, javascript is case sensitive!
var now = new Date()
var regex = new RegExp("^ ?d{1}d{3}$")//Direct quantity of regular expression
object:
var point = {x:12,y:34};
var point2 = {"super":{day:sunday,day1:monday}}//The properties of the object refer to another object.
Conversion of objects:
When a non-empty object is used in a Boolean environment: it is converted to true. When used in a string environment, JavaScript will call the toString() method of the object and use this function The returned value, when used in a numeric environment: JavaScript will call the valueOf() method of the object. If a basic type is returned, this value will be used. Most of the time, the object itself is returned. In this case The JavaScript callback calls the toString() method to convert the object into a string, and then attempts to convert it into a number. I hope everyone will understand the concepts above and use them in the future.
2. Array
<script> <br>var array = new Array(); <br>var arr = new Array(1.2,"Javascript",{x:12,y:23})// <br>Array literal with parameters: <br>var a = [1.2,"Javascript",{x:12,y:23}]//The array is numbered [] and the object is numbered {}, which is easy to remember ! <br></script>
3. Null (empty)
The JavaScript keyword Null is a special value, which means no value. Null is often regarded as a special value of the object type, which means a value without an object. When a variable The value of
is null, then it means that its value is not valid (Array, Object, number, string, Boolean value), details: null is converted to false in a Boolean environment; in a number
environment Convert to 0.
4. Undefined (undefined)
When using an undeclared variable, or using a declared variable but without assigning a value, or using an object property that does not exist, The returned
is the undefined value. In the future (namespaces and modules are still used a lot, everyone needs to understand), details: underfined will be converted to false in the Boolean environment, and it will be converted to false in the digital environment
into NaN. This is different from null. The object encapsulating it is Error.
Summary: Although the above content can be understood at a glance, I hope friends who are beginners like me will not be careless!
JavaScript Basics (2)
Basics
javascript: Declaration of variables
The following are several ways to declare variables
var value;
var value,value1,value2;//Declare multiple variables at the same time, but the values of these variables are all undefined
var i = 0,j = 0,k=100;/ /Variable declaration and initialization integrated.
//If you try to read a variable (value) that does not exist, an error will be reported! But if you try to assign a value to a variable that is not declared using Var, javascript
// will implicitly declare the variable, and the declared variable is still global. Details: So when everyone creates variables, try to use Var
//The scope of the variable (this problem is also easy to happen, everyone needs to understand it)
javascript: The scope of the variable
These It’s all details, and beginners like me must pay attention to avoid it!
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 When the variable name declared in the function body is the same as the global variable name, JavaScript will hide the global variable
var golbal ="areagolbal";
document.write("local is :" local "and golbal is :" golbal "< br />");
}
area();
//Output: local is :arealocaland golbal is :areagolbal
Define local in nested functions variables, what will be the effect? Look below:
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: passing by value and passing by address
This is also an important concept! Don't miss it.
Pass by value and address
Copy the actual copied value , there are different ones. What is copied is only the reference to the number. If passed this
independent copy. The new reference modifies the value, and this change is visible to the original reference as well.
What is passed to the function is an independent copy of the value. What is passed to the function is a reference to the value. If the function
changes it, it has no effect outside the function. If the value is modified by the reference passed to it, this change
Changes are also visible.
Comparison Compares these two opposing values, usually comparing two references one by one to determine whether the
bytes they refer to are compared to determine whether they are equal to the same value.
javascript: basic types and reference types
The basic rules of javascript are: basic types are operated by passing by value, and reference types are operated by passing by address. (What is the value type, or what is the reference, please see 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 has the same effect as
}
//Call the function and pass two parameters (you may think that this function changes the value of the global variable, but in fact it does not. The function also uses 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 calling the function
document.write(array[0] "
");
//Output Javascccp;
//After calling the function
modifyArray(array);
document.write(array[0] "
");
//Output uppercase JAVASCRIPT
// The same effect will be achieved by modifying objarray
objarray[0] = "Frank";
document.write(array[0] "
");
//Output Frank;
Summary: I hope everyone will not miss the above content, it is still very helpful for learning the following knowledge!