Introduction to JavaScript Function function types
// 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!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

Usage and Function of Vue.use Function Vue is a popular front-end framework that provides many useful features and functions. One of them is the Vue.use function, which allows us to use plugins in Vue applications. This article will introduce the usage and function of the Vue.use function and provide some code examples. The basic usage of the Vue.use function is very simple, just call it before Vue is instantiated, passing in the plugin you want to use as a parameter. Here is a simple example: //Introduce and use the plug-in

The file_exists method checks whether a file or directory exists. It accepts as argument the path of the file or directory to be checked. Here's what it's used for - it's useful when you need to know if a file exists before processing it. This way, when creating a new file, you can use this function to know if the file already exists. Syntax file_exists($file_path) Parameters file_path - Set the path of the file or directory to be checked for existence. Required. Return file_exists() method returns. Returns TrueFalse if the file or directory exists, if the file or directory does not exist Example let us see a check for "candidate.txt" file and even if the file

The usage of js function function is: 1. Declare function; 2. Call function; 3. Function parameters; 4. Function return value; 5. Anonymous function; 6. Function as parameter; 7. Function scope; 8. Recursive function.

With the development of the Internet, SOA (service-oriented architecture) has become an important technical architecture in today's enterprise-level systems. Services in the SOA architecture can be reused, reorganized and extended, while also simplifying the system development and maintenance process. As a widely used Web programming language, PHP also provides some function libraries for implementing SOA. Next, we will detail how to use SOA functions in PHP. 1. The basic concept of SOA. SOA is a distributed system development idea and architecture.

The clearstatcache() function is used to clear the file status cache. PHP caches the information returned by the following functions −stat()lstat()file_exists()is_writable()is_readable()is_executable()is_file()is_dir()filegroup()fileowner()filesize()filetype()fileperms() What to do To provide better performance. Syntax voidclearstatecache() Parameter NA Return value clearstatcache(
