// In JS, the Function (function) type is actually an object; each function is an instance of the Function type; and has the same properties and methods as other reference types;
// Because A function is an object, so the function name is actually a pointer to the function object;
1. Declaration method of function
1.函数声明方式 function box(num1,num2){ return num1+num2; } 2.函数表达式定义函数 var box = function(num1,num2){ // 通过变量box即可引用函数; return num1+num2; }; // 注意函数末尾有一个分号,就像声明其他变量时一样; var another = box; // 使用不带圆括号的函数名是访问函数指针;而非调用函数; console.log(another(10,10)); 3.使用Function构造函数 var box = new Function('num1','num2','return num1+num2'); // 第三种方式不推荐,这种语法会导致解析两次代码(第一次解析常规JS代码,第二次解析传入构造函数中的字符串),从而影响性能; // 可以通过这种语法来理解"函数是对象,函数名是指针"的概念;
2. Function as value
// JS中的函数名本身就是变量,所以函数也可以作为值来使用; // 也就是说,不仅可以像传参数一样把一个函数传递给另一个函数,而且可以将一个函数作为另一个函数的结果返回; function box(sumFunction,num){ // 无论第一个参数传递进来的是什么函数, return sumFunction(num); // 它都会返回执行第一参数后的结果; } function sum(num){ return num+10; } // 传递函数到另一个函数里; // 要访问函数的指针不执行函数的话,须去掉函数名后的圆括号; var result = box(sum,10); // =>20;
3. Internal function Attributes
// 函数内部有两个特殊的对象:arguments和this; // 1.arguments:是一个类数组对象,包含着传入函数中的所有参数,主要用途是保存函数参数; // arguments这个对象还有一个名叫callee的属性,该属性是一个指针,指向拥有这个arguments对象的函数; function box(num){ if(num<=1){ return 1; }else{ return num*arguments.callee(num-1); // 使用arguments.callee来执行box本身; } } // 2.this:引用的是函数据以操作的对象,或者说函数调用语句所处的作用域; // 当在全局作用域调用函数时,this对象引用的就是window; window.color = "red"; alert(this.color); // 打印全局的color;=>red; var box = { color:'blue', sayColor:function(){ alert(this.color); // 打印局部的color;=>blue; } };
Four function attributes and methods
// JS中的函数是对象,因此函数也有属性和方法;包含length和prototype; // length属性:表示函数希望接收到命名参数的个数; function box(name,age){ alert(name+age); } alert(box.length); // 2s // prototype属性:它是保存所有实例方法的真正所在,也就是原型; // prototype包含两个方法:apply()和call(),每个函数都包含这两个非继承而来的方法; // 这两个方法的用途都在特定的作用域中调用函数,实际上等于设置函数体内this对象的值; var color = 'red'; var box = { color = 'blue'; } function sayColor({ alert(this.color); }); sayColor(); // 作用域在window; sayColor.call(this); // 作用域在window; sayColor.call(window); // 作用域在window; sayColor.call(box); // 作用域在box,对象冒充;=>red; // 使用call(box)方法的时候,sayColor()方法的运行环境已经变成了box对象里了; // 使用call()或apply()来扩充作用域的最大好处,就是对象不需要与方法发生任何耦合关系; // 耦合:相互关联的意思,扩展和维护会发生连锁反应; // 也就是说,box对象和sayColor()方法之间不会有多余的关联操作,比如:box.sayColor = sayColor; function Animal(){ this.name = "Animal"; this.showName = function(){ alert(this.name); } } function Cat(){ this.name = "Cat"; } var animal = new Animal(); var cat = new Cat(); //通过call或apply方法,将原本属于Animal对象的showName()方法交给对象cat来使用。 //输入结果为"Cat" animal.showName.call(cat,","); //animal.showName.apply(cat,[]);
Five summary
1 // Functions are actually instances of the Function type, so functions are also objects; and this is the most distinctive feature of JavaScript place;
2 // Because of the function object, the function also has methods that can be used to enhance its behavior;
More JavaScript Function function type introduction related articles Please pay attention to PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor