Summary of learning JavaScript Ninja Secrets (Second Edition) (1) -- functions

Oh, ha ha 2020-11-09 16:56:02
summary learning javascript ninja secrets


 The summary of this article and the content of learning , Let's start with chapter three ; The whole book takes about a week to study , In fact, you can finish your study in your spare time .
Basic knowledge of , It needs continuous learning and supplement , If the knowledge you can't use , We'll forget ; Plus I'm not a computer major , So I've been trying to make up for it .

Ways of learning

 There is an old saying ,“ It's just saying it, not doing it ”; So it's boring .
So you still have to tap the code and practice it yourself , It's all very important .

I recommend this one WeChat reading

Because you can... In your spare time , Use your mobile phone to read , also Reading aloud The function of ; sometimes , We are always distracted when we read technical books . You can use it app The recitation function of "let you concentrate more .

Be sure to type the code !!!!

We must learn to sum up by ourselves ; It's also important . Better a good memory than a bad pen , That's why .


JavaScript The electronic version of Ninja Secret script II

yes epub Version of ;《JavaScript Ninja secrets Second Edition .epub》; Reading the electronic version is for the convenience of It's easier for me to operate examples on the computer , Copy it directly , Breaking point .
If you need , So here's the documentation .

link : password : n56j

Let's start our summary and overview today . My title also says , This is a Summary of key contents , yes Convenient for you to quickly remember and recall knowledge points , A better way is to read it systematically .

And then there's something about it , I will introduce the sentences of the article directly , Please take it easy .
I'll sort it out in the order of the catalog

The first 3 Chapter Novice's first function lesson : Definition and parameters

Anything an object can do , Functions can also do .

Functions are also objects , The only thing that's special is that it's callable (invokable), That is, the function is called to perform an action .

Callback function

function useless(ninjaCallback) {
return ninjaCallback();
This function may not work , But it reflects a function's ability , That is, the function is used as an argument to another function , The function is then called with arguments .

Storage function

var store = {
nextId: 1, //⇽--- Track the next function to be copied
cache: {}, //⇽--- Use an object as a cache , We can store functions in it
add: function(fn) {
if (! { = this.nextId++;
this.cache[] = fn;
return true;
} //⇽--- Only if the function is unique , Add the function to the cache
function ninja(){}
"Function was safely added.");
"But it was only added once."); //⇽--- Test that the code above works as expected Will be , Come in fn Unique identification of ; If you pass in the same function , We're going to find this id Is there , It won't be added in .
It's equivalent to modifying this fn Function attribute , Added a unique identifier ID;
If it's not added fn, So there's no such thing ID Mark . = this.nextId++;

Function definition

JavaScript Provides several ways to define functions , Can be divided into 5 class .

Function declaration

 function myFun(){ return 1;}
Each function is declared with Compulsory function start , then Then the mandatory function name , And parentheses and a column of optional parameter names separated by commas .
Function declaration must be Has a function name Because they're separate statements . The basic requirement of a function is that it should be able to be called , So it has to Having a way of being quoted , So the only way to do that is by its name .


Function expression

var myFun = function (){ return 1;}
JavaScript The function in is the first type of object , In addition, it means that they can pass through Literal creation , Sure Assign values to variables and properties , It can be used as an argument passed to another function or the return value of a function .
Be careful ️:
 Function declarations and function expressions are different from each other in code , One more ` The more important difference ` yes :
* Function declaration , Function names are mandatory ;
* Function expression , The function name is completely optional .
Immediate function

The comparison between the call of a standard function and the immediate call of a function expression

Be careful ️: Bracketed function expressions

Function expressions are wrapped in a pair of brackets . Why do you do this ?

The reason is purely grammatical .JavaScript The parser must be able to Distinguish between function declaration and function expression The difference between
If you remove the bag Brackets around function expressions , Make the immediate call a separate statement function() {}(3),JavaScript Start The parsing will end , Because the independent sentence is function start , Then the parser thinks it's dealing with a function declaration . Each function declaration must have a name ( However, there is no specific name here ), So that's where the program goes Will report a mistake . To avoid mistakes , Function expressions should be enclosed in brackets , by JavaScript The parser indicates that it is processing a function expression rather than a statement .


Will report an error directly , Because this function declaration is a form of error .

If you put brackets , Or the unary operator

Function expressions should be enclosed in brackets , by JavaScript The parser indicates that it is processing a function expression rather than a statement .
There is also a relatively simple alternative (function(){}(33)) Can achieve the same goal ( But it's a little strange , So it's not often used ). Put the definition and call of immediate function in brackets , It can also be JavaScript The parser indicates that it is processing function expressions .
Unlike using parentheses to distinguish function expressions from function declarations , Here we use the unary operator +、-、! and ~. This practice is also used to JavaScript The engine indicates that it is dealing with expressions , Instead of statements .

Arrow function

const myFun =()=>{ return 1;}
var greet = name => "Greetings " + name; //⇽--- Define the arrow function ”


Notice the new operator —— Fat arrow symbol =>( The equal sign is followed by a greater than sign ) Is the core of defining arrow functions .

Function constructors

new Function('a', 'b', 'return a + b')

Generator function

function* myGen(){ yield 1; }

The actual and formal parameters of a function

  • A formal parameter is a variable that we list when we define a function .
  • Arguments are the values we pass to the function when we call it .


Function parameters are specified when the function is defined , And all types of functions can have formal parameters .

Excerpt from : [ beautiful ] John Resig Bear Bibeault Josip Maras. “JavaScript Ninja Secret ( The first 2 edition ).” iBooks.

  • Function declaration (skulk Functional ninja Shape parameter ).
  • Function expression (performAction Functional person and action Shape parameter ).
  • Arrow function ( Shape parameter daimyo).

On the other hand , The argument is related to the call of the function . They are the values passed to the function when the function is called .

  • character string Hattori Pass to a function in the form of a function argument skulk.
  • character string Oda Nobunaga Pass to a function in the form of a function argument rule.
  • skulk The formal parameter of the function ninja Pass as an argument to a function performAction.
It can be understood in this way :
Function declaration and function expression , Arrow function , In the declaration, there are formal parameters in brackets ,
When a function is called , The arguments passed to the function are

The remaining parameters

function multiMax(first, ...remainingNumbers) { ⇽--- The remaining parameters are … Prefix
var sorted = remainingNumbers.sort(function(a, b) {
return b – a; ⇽--- Sort the remaining parameters in descending order
return first * sorted[0];
assert(multiMax(3, 1, 2, 3) == 9, ⇽--- Function calls are similar to other functions
"3*3=9 (First arg, by largest.)")

Prefix the last named argument of the function with an ellipsis (...) Prefix , This parameter becomes an array called the remaining parameters , The array contains the remaining parameters passed in .

Be careful ️: Only the last parameter of a function can be a residual parameter .

ES6 How to handle default parameters in

function performAction(ninja, action = "skulking"){ ⇽--- ES6 You can assign values to the formal parameters of a function
return ninja + " " + action;
assert(performAction("Fuma") === "Fuma skulking",
"The default value is used for Fuma");
assert(performAction("Yoshi") === "Yoshi skulking",
"The default value is used for Yoshi");
assert(performAction("Hattori") === "Hattori skulking",
"The default value is used for Hattori"); ⇽--- If there is no input value , Use default parameters
assert(performAction("Yagyu", "sneaking") === "Yagyu sneaking",
"Yagyu can do whatever he pleases, even sneak!"); ⇽--- Using the parameters passed in 


  • hold JavaScript As a functional language, you can write complex code .
  • As the first kind of object , Functions and JavaScript Same as other objects in . Similar to other object types , Function has the following functions .
  • Create... By literals .
  • Assign to a variable or property .
  • Pass as a function parameter .
  • Return... As the result of the function .
  • Assign values to properties and methods .
  • The callback function is followed by the code “ Come back and call ” Function of , It's a very common function , Especially in the event processing scenario .
  • Functions have properties , And these properties can be used to store any information , We can use this feature to do a lot of things ; for example :
  • You can store another function in the function properties for subsequent references and calls .
  • You can create a cache with function properties ( memory ), Used to reduce unnecessary calculations .
  • There are many different types of functions : Function declaration 、 Function expression 、 Arrow function and function generator, etc .
  • Function declaration and function expression are the two main types of functions . Function declaration must have function name , It must also exist as a separate statement in the code . A function expression may not have to have a function name , But at this point it has to be part of other statements .
  • The arrow function is JavaScript A new feature of ,“ This feature allows us to define functions in a more concise way .
  • When the definition of a function is a variable , The argument is the value passed to the function when the function is called .
  • The length of the formal parameter list and the argument list of the function can be different .
  • An unassigned parameter is evaluated to get undefined.
  • The extra arguments passed in are not assigned to any of the named parameters .
  • The remaining and default parameters are JavaScript New features .
  • The remaining parameters —— Additional arguments that do not match any parameter name can be referenced by the remaining arguments .
  • Default parameters —— When a function is called , If no parameters are passed in , The default parameter can give the function the default parameter value .
Excerpt from : [ beautiful ] John Resig Bear Bibeault Josip Maras. “JavaScript Ninja Secret ( The first 2 edition ).” iBooks.
本文为[Oh, ha ha]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云