admin 管理员组文章数量: 887021
【JavaScript
1、严格模式(每次写js之前都要写
//严格模式
'use strict';
2、三种函数表达
1)函数声明
//计算年龄的函数,基于给定的出生年份
//函数声明
//function 函数名(参数){};
function calcAge1(birthYear) {return 2037 - birthYear;
}
const age1 = calcAge1(1991);
console.log(age1); //输出46
2)匿名函数
//匿名函数
//函数表达式
//变量类型 名称 = function(参数){};
const calcAge2 = function (birthYear) {return 2037 - birthYear;
}
const age2 = calcAge2(1991);
console.log(age1);
3)箭头函数
//箭头函数
//变量类型 名称 = (参数) =>{方法};
const calcAge3 = birthYear => 2037 - birthYear;
const age3 = calcAge3(1991);
console.log(age3);//退休
const yearsUntilRetiremet = (birthYear, firstName) => {const age = 2037 - birthYear;const retirement = 65 - age;return `${firstName} retires in ${retirement} years`;
}console.log(yearsUntilRetiremet(1991, 'Jonas')); //还有19年退休
console.log(yearsUntilRetiremet(1980, 'Bobs'));
3、从函数中调用另一个函数
//从函数内部调用另一个函数
const cutPieces = function (fruit) {return fruit * 4;
};const fruitProcessor = function (apples, oranges) {const applPieces = cutPieces(apples);const orangesPieces = cutPieces(oranges);const juice = `Juice with ${applPieces} pieces of apple and ${orangesPieces} pieces of orange.`return juice;
};console.log(fruitProcessor(2, 3)); //2*4=8 3*4=12
const calcAge = function (birthYear) {return 2037 - birthYear;
};const yearsUntilRetiremet = function (birthYear, firstName) {const age = calcAge(birthYear); //调用calcAge,获得年龄const retirement = 65 - age;if (retirement > 0) {console.log(`${firstName} retires in ${retirement} years`);return retirement;} else {console.log(`${firstName} has already retired`);return -1;}
};console.log(yearsUntilRetiremet(1991, "Jonas"));
console.log(yearsUntilRetiremet(1970, "MARK"));
本文标签: javascript
版权声明:本文标题:【JavaScript 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1730749973h1376881.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论