函數(shù)就是一個(gè)功能模塊,函數(shù)式編程是一種面向過程的編程思想,如果遇到一個(gè)大的復(fù)雜問題,可以分解成幾個(gè)部分,每個(gè)部分用專門的函數(shù)分解實(shí)現(xiàn)。
javascript函數(shù)

函數(shù)就是一個(gè)功能模塊,函數(shù)式編程是一種面向過程的編程思想,如果遇到一個(gè)大的復(fù)雜問題,可以分解成幾個(gè)部分,每個(gè)部分用專門的函數(shù)分解實(shí)現(xiàn)。
函數(shù)語法:
function functionName(parameters) {
執(zhí)行的代碼
}
functionName(parameters) // 函數(shù)調(diào)用
函數(shù)聲明后不會(huì)立即執(zhí)行,會(huì)在我們需要的時(shí)候調(diào)用到。
函數(shù)提升:
- 提升(Hoisting)是 JavaScript 默認(rèn)將當(dāng)前作用域提升到前面去的行為。
- 提升(Hoisting)應(yīng)用在變量的聲明與函數(shù)的聲明。
因此,函數(shù)可以在聲明之前調(diào)用:
console.log(add(3, 4));
function add(a, b){
return a + b;
}
函數(shù)表達(dá)式:JavaScript 函數(shù)可以通過一個(gè)表達(dá)式定義。
const add = function(a, b){
return a + b;
}
console.log(add(3, 4));
箭頭函數(shù):表現(xiàn)形式更加簡潔。
const add = (a, b) => {
return a + b;
}
console.log(add(3, 4));
函數(shù)作用域
局部變量:只能在函數(shù)內(nèi)部訪問。
變量在函數(shù)內(nèi)聲明,變量為局部變量,具有局部作用域。
const output = () => {
let a = 10;
}
console.log(a);

變量在函數(shù)外定義,即為全局變量。
全局變量有 全局作用域: 網(wǎng)頁中所有腳本和函數(shù)均可使用。
let url = "https://noi.hioier.com/";
const output = () => {
console.log(url);
}
output();
哥德巴赫猜想

首先,將一個(gè)大問題劃分成兩個(gè)子問題:
- 判斷一個(gè)數(shù)是否是質(zhì)數(shù);
- 循環(huán)遍歷2~n,如果i是質(zhì)數(shù)且n-i也是質(zhì)數(shù),則輸出結(jié)果,并跳出循環(huán)。
因?yàn)槊杜e過程是從小到大,第一個(gè)找到的可行解一定是字典序最小的。
let buf = "";
const is_prime = (n) => {
for(let i = 2; i < n; i++){
if(n % i == 0)
return false;
}
return true;
}
process.stdin.on("readable", function(){
let chunk = process.stdin.read();
if(chunk) buf += chunk.toString();
});
process.stdin.on("end", function(){
let n = parseInt(buf);
for(let i = 2; i <= n; i++){
if(is_prime(i) && is_prime(n - i)){
console.log(`${n} = ${i} + ${n-i}`);
break;
}
}
// console.log(is_prime(n))
});
面向?qū)ο缶幊?/h4>
面向?qū)ο缶幊滔噍^于面向過程編程更適合大型程序設(shè)計(jì)。
類是用于創(chuàng)建對象的模板。我們使用 class 關(guān)鍵字來創(chuàng)建一個(gè)類,類體在一對大括號(hào) {} 中,我們可以在大括號(hào) {} 中定義類成員的位置,如方法或構(gòu)造函數(shù)。
每個(gè)類中包含了一個(gè)特殊的方法 constructor(),它是類的構(gòu)造函數(shù),在創(chuàng)建對象時(shí)自動(dòng)執(zhí)行。
class People{
constructor(name, age){
this.name = name;
this.age = age;
}
output(){
console.log(`My name is ${this.name}, I am ${this.age} years old.`);
}
}
let xiaoming = new People("小明", 10);
xiaoming.output();
繼承:
在子類的構(gòu)造函數(shù)中,只有調(diào)用super之后,才可以使用this關(guān)鍵字。
成員重名時(shí),子類的成員會(huì)覆蓋父類的成員。
class Student extends People{
constructor(name, age, score){
super(name, age);
this.score = score;
}
output(){
console.log(`My name is ${this.name}, I am ${this.age} years old.My total score is ${this.score}.`);
}
}
let xiaohong = new Student("小紅", 8, 300);
xiaohong.output();
靜態(tài)方法和靜態(tài)變量
靜態(tài)方法:在成員函數(shù)前添加static關(guān)鍵字即可。靜態(tài)方法不會(huì)被類的實(shí)例繼承,只能通過類來調(diào)用。
class People{
constructor(name, age){
this.name = name;
this.age = age;
}
output(){
console.log(`My name is ${this.name}, I am ${this.age} years old.`);
}
static current_class_name(){
console.log("People");
}
}
let xiaoming = new People("小明", 10);
// xiaoming.output();
// xiaoming.current_class_name();
People.current_class_name();
靜態(tài)變量:只能通過classname.variablename定義和訪問。
class People{
constructor(name, age){
this.name = name;
this.age = age;
People.color = 'yellow';
}
output(){
console.log(`My name is ${this.name}, I am ${this.age} years old.`);
}
static current_class_name(){
console.log("People");
}
}
console.log(People.color);