Fn简介:
语言的基本构建块。
最重要的概念。
Fn - 一段可以反复使用的代码。
{调用、运行、调用} Fn 的意思都是一样的。
Fn - 可以将数据作为参数,计算后返回数据作为结果。
Fn 调用被执行后返回的结果替换。
Fns 非常适合实施 DRY 原则
参数被传递,参数是占位符,它接收传递给 fn 的值。
Fn 声明与表达式:
Fn 声明以 fn 关键字开头。
Fn 表达式是匿名 fn,存储在变量内。然后存储 fn 的变量充当 fn。
匿名 fn defn 是一个表达式,该表达式产生一个值。 Fn 只是值。
Fn 不是 String、Number 类型。它是一个值,因此可以存储在变量中。
Fn 声明甚至可以在代码中定义之前调用,即它们被提升。这不适用于 Fn 表达式。
Fn 表达式强制用户首先定义 fns,然后再使用它们。一切都存储在变量中。
两者在 JS 中都有自己的位置,因此需要好好掌握。
Fn 表达式 = 本质上是存储在变量中的 fn 值
ES6 附带 Arrow Fn
单行隐式返回,多行需要显式返回。
箭头 fns 没有获得自己的“this”关键字
学习不是一个线性的过程,知识是逐渐积累的。你不可能一次性了解一件事的所有内容。
## Default parameters const bookings = []; const createBooking = function(flightNum, numPassengers=1, price= 100 * numPassengers){ // It will work for parameters defined before in the list like numPassengers is defined before its used in expression of next argument else it won't work. // Arguments cannot be skipped also with this method. If you want to skip an argument, then manually pass undefined value for it which is equivalent to skipping an argument /* Setting default values pre-ES6 days. numPassengers = numPassengers || 1; price = price || 199;*/ // Using enhanced object literal, where if same property name & value is there, just write them once as shown below. const booking = { flightNum, numPassengers, price, }; console.log(booking); bookings.push(booking); } createBooking('AI123'); /* { flightNum: 'AI123', numPassengers: 1, price: 100 } */ createBooking('IN523',7); /* { flightNum: 'IN523', numPassengers: 7, price: 700 } */ createBooking('SJ523',5); /* { flightNum: 'SJ523', numPassengers: 5, price: 500 } */ createBooking('AA523',undefined, 100000); /* { flightNum: 'AA523', numPassengers: 1, price: 100000 } */
从一个 fn 内部调用另一个 fn:
支持 DRY 原则的非常常见的技术。
支持可维护性
return 立即退出 fn
return = 从fn输出一个值,终止执行
fn 的三种写法,但工作方式相似,即输入、计算、输出
参数 = 接收 i/p 值的占位符,如 fn 的局部变量。
传递参数的工作原理,即值与引用类型
原语按值传递给 fn。原始价值保持不变。
对象通过 fn 的引用传递。原始值已更改。
JS 没有通过引用传递值。
不同 fns 与同一对象的交互有时会产生麻烦
fns 在 JS 中是一流的,因此我们可以编写 HO fns。
Fns 被视为值,只是对象的另一种“类型”。
由于对象是值,因此 fns 也是值。因此,也可以存储在变量中,附加为对象属性等。
此外,fns 也可以传递给其他 fns。前任。事件监听器-处理程序。
从 fns 返回。
Fns 是对象,对象在 JS 中有自己的方法-属性。因此,fn 可以具有可以调用它们的方法和属性。例如调用、申请、绑定等
高阶 Fn :接收另一个 fn 作为参数的 Fn,返回一个新的 fn 或两者。唯一可能的原因是 fns 在 JS 中是一流的
回调 fn 中传入的 Fn,将由 HOF 调用。
返回另一个 fn 的 Fns,即在闭包中。
First class fns 和 HOF 是不同的概念。
回调 Fns 广告:
允许我们创建抽象。更容易看到更大的问题。
将代码模块化为更小的块以供重复使用。
// Example for CB & HOF: // CB1 const oneWord = function(str){ return str.replace(/ /g,'').toLowerCase(); }; // CB2 const upperFirstWord = function(str){ const [first, ...others] = str.split(' '); return [first.toUpperCase(), ...others].join(' '); }; // HOF const transformer = function(str, fn){ console.log(`Original string: ${str}`); console.log(`Transformed string: ${fn(str)}`); console.log(`Transformed by: ${fn.name}`); }; transformer('JS is best', upperFirstWord); transformer('JS is best', oneWord); // JS uses callbacks all the time. const hi5 = function(){ console.log("Hi"); }; document.body.addEventListener('click', hi5); // forEach will be exectued for each value in the array. ['Alpha','Beta','Gamma','Delta','Eeta'].forEach(hi5);
Fns 返回另一个 fn。
// A fn returning another fn. const greet = function(greeting){ return function(name){ console.log(`${greeting} ${name}`); } } const userGreet = greet('Hey'); userGreet("Alice"); userGreet("Lola"); // Another way to call greet('Hello')("Lynda"); // Closures are widely used in Fnal programming paradigm. // Same work using Arrow Fns. Below is one arrow fn returning another arrow fn. const greetArr = greeting => name => console.log(`${greeting} ${name}`); greetArr('Hola')('Roger');
“只有彻底理解某个主题,你才会进步”
调用(),应用(),绑定():
用于通过显式设置“this”关键字的值来设置其值。
call - 在“this”关键字的值之后获取参数列表。
apply - 在“this”关键字的值之后接受一个参数数组。它将从该数组中获取元素,并将其传递给函数。
bind():该方法创建一个新函数,并将 this 关键字绑定到指定对象。无论函数如何调用,新函数都会保留 .bind() 设置的 this 上下文。
call() 和 apply():这些方法使用指定的 this 值和参数调用函数。它们之间的区别在于 .call() 将参数作为列表,而 .apply() 将参数作为数组。
const lufthansa = { airline: 'Lufthansa', iataCode: 'LH', bookings: [], book(flightNum, name){ console.log(`${name} booked a seat on ${this.airline} flight ${this.iataCode} ${flightNum}`); this.bookings.push({flight: `${this.iataCode} ${flightNum}`, name}); }, }; lufthansa.book(4324,'James Bond'); lufthansa.book(5342,'Julie Bond'); lufthansa; const eurowings = { airline: 'Eurowings', iataCode: 'EW', bookings: [], }; // Now for the second flight eurowings, we want to use book method, but we shouldn't repeat the code written inside lufthansa object. // "this" depends on how fn is actually called. // In a regular fn call, this keyword points to undefined. // book stores the copy of book method defuned inside the lufthansa object. const book = lufthansa.book; // Doesn't work // book(23, 'Sara Williams'); // first argument is whatever we want 'this' object to point to. // We invoked the .call() which inturn invoked the book method with 'this' set to eurowings // after the first argument, all following arguments are for fn. book.call(eurowings, 23, 'Sara Williams'); eurowings; book.call(lufthansa, 735, 'Peter Parker'); lufthansa; const swiss = { airline: 'Swiss Airlines', iataCode: 'LX', bookings: [] } // call() book.call(swiss, 252, 'Joney James'); // apply() const flightData = [652, 'Mona Lisa']; book.apply(swiss, flightData); // Below call() syntax is equivalent to above .apply() syntax. It spreads out the arguments from the array. book.call(swiss, ...flightData);
以上是功能的详细内容。更多信息请关注PHP中文网其他相关文章!

Python和JavaScript的主要区别在于类型系统和应用场景。1.Python使用动态类型,适合科学计算和数据分析。2.JavaScript采用弱类型,广泛用于前端和全栈开发。两者在异步编程和性能优化上各有优势,选择时应根据项目需求决定。

选择Python还是JavaScript取决于项目类型:1)数据科学和自动化任务选择Python;2)前端和全栈开发选择JavaScript。Python因其在数据处理和自动化方面的强大库而备受青睐,而JavaScript则因其在网页交互和全栈开发中的优势而不可或缺。

Python和JavaScript各有优势,选择取决于项目需求和个人偏好。1.Python易学,语法简洁,适用于数据科学和后端开发,但执行速度较慢。2.JavaScript在前端开发中无处不在,异步编程能力强,Node.js使其适用于全栈开发,但语法可能复杂且易出错。

javascriptisnotbuiltoncorc; saninterpretedlanguagethatrunsonenginesoftenwritteninc.1)javascriptwasdesignedAsalightweight,解释edganguageforwebbrowsers.2)Enginesevolvedfromsimpleterterterpretpreterterterpretertestojitcompilerers,典型地提示。

JavaScript可用于前端和后端开发。前端通过DOM操作增强用户体验,后端通过Node.js处理服务器任务。1.前端示例:改变网页文本内容。2.后端示例:创建Node.js服务器。

选择Python还是JavaScript应基于职业发展、学习曲线和生态系统:1)职业发展:Python适合数据科学和后端开发,JavaScript适合前端和全栈开发。2)学习曲线:Python语法简洁,适合初学者;JavaScript语法灵活。3)生态系统:Python有丰富的科学计算库,JavaScript有强大的前端框架。

JavaScript框架的强大之处在于简化开发、提升用户体验和应用性能。选择框架时应考虑:1.项目规模和复杂度,2.团队经验,3.生态系统和社区支持。

引言我知道你可能会觉得奇怪,JavaScript、C 和浏览器之间到底有什么关系?它们之间看似毫无关联,但实际上,它们在现代网络开发中扮演着非常重要的角色。今天我们就来深入探讨一下这三者之间的紧密联系。通过这篇文章,你将了解到JavaScript如何在浏览器中运行,C 在浏览器引擎中的作用,以及它们如何共同推动网页的渲染和交互。JavaScript与浏览器的关系我们都知道,JavaScript是前端开发的核心语言,它直接在浏览器中运行,让网页变得生动有趣。你是否曾经想过,为什么JavaScr


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

记事本++7.3.1
好用且免费的代码编辑器

WebStorm Mac版
好用的JavaScript开发工具