本文是金旭亮老师网易云课堂的课程笔记,记录下来,以供备忘
1 //调用函数时传入的实际参数 2 function add(num1, num2) { 3 return num1 + num2; 4 } 5 //一切正常的调用方式 6 console.info(add(1, 2)); //3 7 //不检查参数的类型,字串和数字可以混用 8 console.info(add("1", 2)); //12 9 //多余参数被忽略10 console.info(add(1, 2, 3)); //311 //少的参数被视为undefined12 //1+undefined=NaN13 console.info(add(1)); //NaN
判断是否为 undefined
1 //检测参数是否缺失2 function sayHello(name, message) {3 if (typeof message === 'undefined') {4 message = '你好!';5 }6 console.info(name + "," + message);7 }8 sayHello("贾君鹏", "你妈喊你吃饭"); 9 sayHello("贾君鹏"); //贾君鹏,你好!
1 //保存参数的arguments对象 2 function sumNumbers() { 3 var result = 0; 4 for (var i = 0; i < arguments.length; i++) { 5 result += arguments[i]; 6 } 7 return result; 8 } 9 console.info(sumNumbers(1, 2)); //310 console.info(sumNumbers(1, 2, 3)); //6
1 //函数对象的length属性 2 function sayName(name){ 3 console.info(name); 4 } 5 function sum(num1, num2){ 6 return num1 + num2; 7 } 8 function sayHi(){ 9 console.info("hi");10 }11 console.info(sayName.length); //112 console.info(sum.length); //213 console.info(sayHi.length); //0
1 //作为参数的函数对象2 function callSomeFunction(func, argu) {3 return func(argu);4 }5 function getGreeting(name) {6 return "Hello, " + name;7 }8 var result = callSomeFunction(getGreeting, "jxl");9 console.info(result); //Hello,jxl
1 var callTwice = function (otherFunc) {2 otherFunc();3 otherFunc();4 };5 callTwice(function () {6 console.info("this is a function");7 });
1 <script>2 var button = document.getElementById('btnClick');3 var result = document.getElementById('result');4 var clickCount = 0;5 button.addEventListener('click', function () {6 clickCount++;7 result.setAttribute('value', clickCount+': Hello,world!');8 })9 </script>
运行:
新闻热点
疑难解答