一文详解javascript函数和面向对象编程

2023-05-27 0 246

javascript函数

函数是两个模块,函数式程式设计是一类面向全国操作过程的程式设计价值观,假如碰到两个大的繁杂难题,能还原成两个部份,每一部份用专门针对的函数降解同时实现。

一文详解javascript函数和面向对象编程

函数句法:

function functionName(parameters) {继续执行的标识符functionName(parameters) // 函数初始化

函数新闻稿后不能立刻继续执行,会在他们须要的这时候初始化到。

函数提高:

提高(Hoisting)是 JavaScript 预设将现阶段返回值提高到后面去的犯罪行为。提高(Hoisting)应用领域在函数的新闻稿与函数的新闻稿。

因而,函数能在新闻稿以后初始化:

console.log(add(3, 4));function add(a, b){return a + b;

函数函数:JavaScript 函数能透过两个函数表述。

const add = function(a, b){return a + b;console.log(add(3, 4));

斜线函数:表达方式更为简约。

const add = (a, b) => {return a + b;console.log(add(3, 4));函数返回值

局部性函数:根本无法在函数外部出访。

函数在函数内新闻稿,函数为局部性函数,具备局部性返回值。

const output = () => {let a = 10;console.log(a);

一文详解javascript函数和面向对象编程

函数在函数外表述,即为全局变量。

全局函数有全局返回值: 网页中所有脚本和函数均可使用。

let url = “https://noi.hioier.com/”;const output = () => {console.log(url);output();哥德巴赫猜想

一文详解javascript函数和面向对象编程

首先,将两个大难题划分成两个子难题:

判断两个数是否是质数;循环遍历2~n,假如i是质数且n-i也是质数,则输出结果,并跳出循环。

因为枚举操作过程是从小到大,第两个找到的可行解一定是字典序最小的。

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))面向全国对象程式设计

面向全国对象程式设计相较于面向全国操作过程程式设计更适合大型程序设计。

一文详解javascript函数和面向对象编程

类是用于创建对象的模板。他们使用 class 关键字来创建两个类,类体在一对大括号{}中,他们能在大括号 {} 中表述类成员的位置,如方法或构造函数。

每一类中包含了两个特殊的方法 constructor(),它是类的构造函数,在创建对象时自动继续执行。

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();

继承:

在子类的构造函数中,只有初始化super之后,才能使用this关键字。

成员重名时,子类的成员会覆盖父类的成员。

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();静态方法和静态函数

静态方法:在成员函数前添加static关键字即可。静态方法不能被类的实例继承,根本无法透过类来初始化。

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();

静态函数:根本无法透过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);

Javascript简介和基础数据类型Javascript对象和选项卡同时实现该文简述javascript轮播图该文简述JasName属性和slice切片

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务