One: javascript object
1. Object-based operation (object-oriented encapsulation, inheritance, polymorphism)
2. Use an object to complete related operations as a whole
String Object; Book Order Object; Array; Time Object
the content of the object
1: attribute (variable)
2: Method (function)
are related to this object
1: Declare the object
2: Use objects
3: Using System Objects
step:
1: find the object
2: Instantiate the object
3: Operation object
object instance. property (cancel, assign)
object instance ["attribute name"]
object instance.method()
custom object
var dt=new Date(); var str="今天是:"; str+=dt.getFullYear()+"年"; str+=(dt.getMonth()+1)+"月"; str+=dt.getDay()+"日"; str+=dt.getHours()+":"; str+=dt.getMinutes()+":"; str += dt . getSeconds ()+ "week" ; switch (dt.getDay()){ case 0: str+="日"; break; case 1: str+"一"; break; case 2: str+"二"; break; case 3: str+"三"; break; case 4: str+"四"; break; case 5: str+"五"; break; case 6: str+"六"; break; } document.write(str);
Two: functions in javascript
Function: is a "named" piece of code that performs a specified function.
Functions can only be used by calling, calling: by name (either before or after declaration)
test(); //value
function test(a,b){
Function segment (one, or more)
return value
javascript is object based (everything is the use of objects)
}
test();
1: function name
2: Parameters: Use parameters as needed
3: Function body (function)
4: return value
If the function name does not add (), the function name represents the entire function
Callback
condition:
1. The role of the function decides whether I use it or not
2. The parameters of the function determine how I use it
3. The return value determines how my call number is handled
Three: binary search
<html>
<script>
function binary_search(arr,low,high,key){
if(low>high){
return -1;
}
var mid=parseInt((high+low)/2); //查找结束判定条件
if(arr[mid]==key){
return mid;
}else if(arr[mid]>key){
high=mid-1;
return binary_search(arr,low,high,key);
}else if(arr[mid]<key){
low=mid+1;
return binary_search(arr,low,high,key);
}
};
var arr=[1,2,3,4,5,6,7,8,9,10,11,23,44,86];
var result=binary_search(arr,0,13,10);
alert(result);
</script>
</html>