<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>javascript 函数</title>
<link rel="stylesheet" type="text/css" href="css/css_test1.css">
</head>
<body>
<hr>
<h3>javascript 函数</h3>
<p id="demo1">自调用函数</p>
<script>
(function (){
//自调用函数
document.getElementById("demo1").innerHTML="自动调用函数";
})();
function multiplyFunction(a,b){
return a*b;
}
var result1=multiplyFunction(3,9);
console.log(result1);
function argumentFunction(a,b){
return arguments.length; //回函数调用过程接收到的参数个数
}
console.log(argumentFunction(2,10));
function addFunction(a,b){
return a+b;
}
//函数作为一个字符串返回
console.log(addFunction.toString());
var result2=function(a,b){
return a-b;
}
console.log(result2(90,10));
//箭头函数
const a=(a,b)=>a/b;
console.log(a(54,6));
var array1=[3,6,7,8,9];
target1=9
function returnTwoIndex(nums,target){
len_nums=nums.length;
for (let i=0; i<len_nums-1; i++){
for (let j=i+1; j<len_nums; j++){
if (nums[i]+nums[j]==target) {
var result=[i,j];
return result;
}
}
}
}
console.log(returnTwoIndex(array1,target1));
//构造函数
function returnName(arg1,arg2){
this.firstName=arg1;
this.lastName=arg2;
}
var member10=new returnName("Mane","Sadio");
console.log(member10.lastName);
function getMaxNumber(a,b){
return a>b ? a:b;
}
var result3=getMaxNumber.call(result3,3,8);
console.log(result3);
var testArray1=[7,2];
var result4=getMaxNumber.apply(result4,testArray1);
console.log(result4);
</script>
<hr>
<h3>JavaScript 闭包</h3>
<button type="button" onclick="testCutData()">减数</button>
<p id="demo6">100</p>
<script>
//内嵌函数
function makeData(){
var counter=0;
function plusData(){
counter+=1;
}
plusData();
return counter;
}
//闭包
var cut=(function(){
var score=100;
return function(){
return score-=1;
}
})();
function testCutData(){
document.getElementById("demo6").innerHTML=cut();
}
</script>
<br><br><br><br>
</body>
</html>
javascript 函数
最新推荐文章于 2024-11-04 16:17:23 发布