js foundation one

<html>
<head>
<meta http-equiv="refresh" content="2">
<script type="text/javascript"> var var01=1; function funtst(){ document.write(var01);//undefined var var01=0; }






funtst();

                    /* Equivalent to:

                              function funtst(){

                                    var var01;

document.write(var01);//undefined

                                        var01=0;

}

                        */

function funtst1(){

                                var var01 = 0;

                                document.write(var01);// 0


}
/*
Main reason: js engine precompile.
1. Detect that the variable has been declared inside the function
a: If the declaration is before the statement, if the value is assigned, output the value, otherwise output undefined
b: If the declaration is after the statement (this situation is called hoisting) , output undefined;
2. If there is no declaration of the variable inside the detection function, it will be processed according to the global variable.
*/

/**
javascript basic syntax 1. javascript
is a scripting language, a weakly typed, dynamically typed, object-based programming language, interpreted and executed.
2. javascript is used to operate html, css, and can make web pages display dynamically.
3.Js syntax:
usually written in the head tag <script>
4. Variable naming rules:
(1) Variable names must start with a letter or an underscore.
(2) Variables can contain numbers, uppercase and lowercase letters from A to Z.
(3) JavaScript is case-sensitive.
5. Since js is a weak language, var is used to declare all type variables. 6. js
data type
Number specially saves the number type
String specially saves the string type
boolean specially saves the logical true and false type
undefined specially saves the declared variable but does not assign an initial value
null means do not point to any address.
Global variables do not need to be declared with the var keyword, but local variables must
be declared with this keyword.
Note: Assigning a value to a variable that has never been declared will not make an error, it will automatically create a variable with the same name and save the data.
Variable scope:
Local variables are freed after the function runs.
Global variables are freed after the page is closed.
7. Operators
(1) Arithmetic comparators
+, - ,* ,/,%,++,--
i++: first assign value, and then auto-increment to participate in the operation.
++i: Self-increment first, and participate in the operation in assignment.
(2) Relational operator
== ! = => =< 
(3) Logical operator
&& || !
8. Conditional statements if and if-else
If (judgment condition) {
when the judgment condition is true, the code block to be executed
} else if (if the condition is not satisfied, go here to judge){
the condition in Else if is true When, execute the code block
}else{
all other cases, execute the code block
}
1. switch case statement
(1) syntax:  
switch (expression) {
case constant: js statement; break;
case constant: js statement; break;
...
default: js statement; break;
(2) Execution process:
first execute the value of the expression, then go to the curly brackets to find the corresponding case, and after finding it, execute the corresponding js statement, if there is no keyword break, it will continue to execute the js statement in the subsequent case until the end of the switch, and it will jump out directly when the keyword break is encountered. If the corresponding case cannot be found, execute the statement following the default;
2. Loop (three kinds of Loop structure: for while do-while)
(1) For loop:
① Syntax:
for (expression 1; expression 2; expression 3; ) {
loop body
}
Notes:
expression 1 initialization of loop variables;
expression 2 Loop condition;
expression 3 The value of the loop variable changes;
loop body: the statement that needs to be repeated;
② Execution process
1 Execute expression 1 first;
2 Judge the truth of expression 2, if it is true, execute step 3, otherwise loop End;
3 Execute the loop body;
4 Execute expression 3;
5 Start again from step 2;
(2) while loop
① Syntax:
while(expression) {
 loop body
}
② Execution process: First judge the true or false of the expression, if true, execute the loop body;
otherwise, the loop ends.
(3) do-while loop
① Syntax:
do {
loop body
} while(expression) ;     
② Execution process: First execute the loop body, and then judge whether the expression is true or false.
If true, continue executing the loop body, otherwise end the loop.
③ The difference between do-while and while: while loop may not be executed once, do-while will be executed at least once.
3. Array
(1) One-dimensional array:
a. var arry=[3,4,5]//Define a one-dimensional array, the array elements are 3, 4, 5
b. Use the Array built-in object 
var arry=new Array( 5)//Define a one-dimensional array, the array length is 5, the current element value of the array is undefined
var arry=new Array(3,4,5)//Define a one-dimensional array, the array elements are 3, 4, 5
( 2) Two-dimensional array
a.var arry=[[1,2,3],[4,5,6]]//Define a two-dimensional array, the array elements are
1 2 3
4 5 6
b.var arry = new Array(4); //declare one-dimensional
for(var k=0;k<arry.length;k++){    
arry[k]=new Array(3); //Declare two-dimensional, one element in each one-dimensional array is an array;
for(var j=0;j<arry[k].length;j++){ 
 array[k][j]=(Math.random())*10;    
}
}
Common properties: length Returns the length of the array
Common methods:
join (separator)
separator: Optional, specify the separator to be used. If this parameter is omitted, a comma is used as the delimiter.
Puts all the elements in the array into a string, the elements are separated by the specified delimiter.
reverse()
reverses the elements in the array and returns the array
sort(sortby)
sortby: optional, specifies the sort order, must be a function.
Sort the array.
push()
adds an element to the end of the array and returns the new length of the array.
unshift()
adds an element to the beginning of the array and returns the new length. sequence. Must be a function.
Note: This method does not work correctly in IE browser and displays as undefined.
pop()
removes the last element in the array and returns with it.
shift()
removes the first element in the array and returns with it.
4. Function
(1) Function definition (the parameter list does not need to define the type)
①function method name (parameter list){
method body;
} ②var method
name=function (parameter list){
method body;
}
function call: method name();
5.js object
(1) String object
①String object creates
a.var str=”abc”;
b.var str=new String(“abc”); ②Attribute
: length Returns the length of the string ③Common
method:
indexOf(searchvalue,fromindex)
searchvalue: required. Specifies the string value to retrieve.
fromindex: optional integer parameter. Specifies the position in the string at which to start searching. If this parameter is omitted, the search will start from the first character of the string.
Returns the position of the first occurrence of the specified string in the string.
substring(start,stop)
start: required, a non-negative integer specifying the position of the first character of the substring to be extracted in the main string.
stop: optional. A non-negative integer that is one more than the position in the main string of the last character of the substring to be extracted. If this parameter is omitted, the returned substring will continue until the end of the string.
The substring returned by this method includes the characters at start but not the characters at end.
substr(start,length)
start: Required, the starting subscript of the substring to be extracted. Must be a numeric value. If negative, the parameter specifies the position from the end of the string. That is, -1 refers to the last character in the string, -2 refers to the second-to-last character, and so on. (A negative value, IE browser will not work properly).
length: optional. The number of characters in the substring. Must be a numeric value. If this parameter is omitted, the string from the beginning to the end of the main string is returned.
(3) Math object
Attribute : PI Pi π
Common methods:
random() Returns a random number between 0 and 1.
round(n) rounds the number n to the nearest whole number.
var number=Math.random();
//Round o,1
var numberInt=Math.round(number*8);
//js object, browser object, dom object (three objects)
//String string: indexOf ,substr,subString
*/
var number=0;
for(var i=0;i<8;i++){
number=number+8;
document.write("This is the "+i+"th loop"+"<br />");
if(i==7){
document.write("End of loop");
}
}
document.
var n=4;
for(var i=0;i<2*n-1;i++){
for(var j=0;j<2*n-1;j++){
if((i<n)&&(j<(n-i-1))||(j>=(n+i))||(i>=n)&&(j<=(i-n)||(j>=(3*n-i-1-1)))){
document.write("  ");
}else{
document.write("*");
}
}
document.write("<br/>")
}
/*兔子问题*/
var month=5;
var f1=1,f2=1;
for(var i=2;i<month;i++){
var temp=f2;
f2=f1+f2;
f1=temp;
}
document.write(month+"月兔子总数为:"+f2+"<br/>");
var array=[[1,3,3],[2,5,6],[0,8,9]];
var array1=new Array(4,1,2,4,5);
var array2=[1,3,5,7];
for(var i=0;i<array2.length;i++){
document.write(array2[i]+"<br/>");
}
//alert(array1.unshift(0));
function myJoin(array){
var str="";
for(var i=0;i<array.length;i++){
str=str+array[i];
}
document.write(str+"<br/>"); } myJoin(array1); function myReverse(array){ var length=array.length; for(var i=0;i<array.length/2;i++){ array[i]=array[i]^array[length-1-i]; array[length-1-i]=array[i]^array[length-1-i]; array[i]=array[i]^array[length-1-i]; } document.write(array1+"<br/>"); } myReverse(array1); function mySort(array){ for(var i=0;i<array.length-1;i++){















for(var j=0;j<array.length-1-i;j++){
if(array[j]>array[j+1]){
array[j]=array[j]^array[j+1];
array[j+1]=array[j]^array[j+1];
array[j]=array[j]^array[j+1];
}
}
}
document.write(array);
}
mySort(array1);
//document.write(Myjoin(array1));
/*alert(array.join());
alert(array.reverse());
alert(array.sort());
alert(array.push(array1));*/
function caculate(operator){
var num1=document.myName.myNumber1.value;
var num2=document.myName.myNumber2.value;
if(operator=="/"&&num2==0){
alert("除数不能为零,请重新输入");
}else{
var result=eval(num1+operator+num2);
document.myName.myResult.value=result;
}
}
var stringMine="acdefg";
document.write(stringMine.indexOf("c"),0);
var number=Math.random();
//四舍五入 o,1
var numberInt=Math.round(number*8);
document.write("<img src='jygbn'>"+numberInt);


</script>
<style type="text/css">
.btn{
width:50px;
height:50px;
margin:10px;
}
body{
  margin-left:30%;
 }
</style>
</head>
<body>
<form name="myName">
<input type="button" name="test" id="test" onClick="funtst()" value="test"/><br/>
数字1:<input type="text" name="myNumber1" id="" value=""/><br/>
数字2:<input type="text" name="myNumber2" id="" value=""/><br/>
<input type="button" class="btn" value="+"  onclick="caculate(this.value)" border="100px" />
<input type="button" class="btn" value="-"  onclick="caculate(this.value)" />
<input type="button" class="btn" value="*"  onclick="caculate(this.value)" />
<input type="button" class="btn" value="/"  onclick="caculate(this.value)" /><br/>
计算结果:<input type="text" name="myResult" id="" value=""/><br/>
<form> </body> </html>



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326396855&siteId=291194637