理解Javascript_08_函数对象_javascript技巧
函数对象
首先,大家得明确一个概念:函数就是对象,代表函数的对象就是函数对象。既然是对象,那它又是被谁构造出来的呢?下面我们来看一段描述:JavaScript代码中定义函数,或者调用Function创建函数时,最终都会以类似这样的形式调用Function函数:var newFun=Function(funArgs, funBody); 。由此可知函数对象是由Function这个函数对象构造出来的。
注:Function对象本身也是一个函数,因此它也一个函数对象。关于Function的深入理解,请见后续博文。
正面我们来看一段代码:
//定义方式一
function func(x) {
alert(x);
}
//定义方式二
var func = function(x) {
alert(x);
};
//实际执行
var func = new Function(“x”, “alert(x);”);
通过上面的代码可知,函数func无非是由Function对象接收两个参数后构造出来的而矣!
注:关于定义方式一与定义方式二的区别,请见后续博文
函数对象的创建过程
函数对象详细创建步骤如下:
1. 创建一个build-in object对象fn
2. 将fn的内部[[Prototype]]设为Function.prototype
3. 设置内部的[[Call]]属性,它是内部实现的一个方法,处理函数调用的逻辑。(简单的理解为调用函数体)
4. 设置内部的[[Construct]]属性,它是内部实现的一个方法,处理逻辑参考对象创建过程。(简单的理解为创建对象《理解Javascript_06_理解对象的创建过程》一文)
5. 设置fn.length为funArgs.length,如果函数没有参数,则将fn.length设置为0
6. 使用new Object()同样的逻辑创建一个Object对象fnProto
7. 将fnProto.constructor设为fn
8. 将fn.prototype设为fnProto
9. 返回fn
步骤1跟步骤6的区别为,步骤1只是创建内部用来实现Object对象的数据结构(build-in object structure),并完成内部必要的初始化工作,但它的[[Prototype]]、[[Call]]、[[Construct]]等属性应当为 null或者内部初始化值,即我们可以理解为不指向任何对象(对[[Prototype]]这样的属性而言),或者不包含任何处理(对 [[Call]]、[[Construct]]这样的方法而言)。步骤6则将按照《理解Javascript_06_理解对象的创建过程》创建一个新的对象,它的 [[Prototype]]等被设置了。
从上面的处理步骤可以了解,任何时候我们定义一个函数,它的prototype是一个Object实例,这样默认情况下我们创建自定义函数的实例对象时,它们的Prototype链将指向Object.prototype。
注:Function一个特殊的地方,是它的[[Call]]和[[Construct]]处理逻辑一样。深层次的原因将在后续博文中介绍。
下面我们写一些用例脚本来测试一下上面的理论:
function Animal(){
}
alert(Animal.length);//0
var dog = new Animal();
这个JS证明了步骤5的正确性。最后,还是来看一下函数对象的内存图,简单起见,内存图只描述了Animal的构造过程:

来自于一个整体的分析图:

图片本身已经能解释很多很多的问题了,结合前面instanceof原理,对象构造原理,原型链原理,自已去体会吧,我就不多说什么了。
其实上Function对象是一个很奇妙的对象,它与Object的关系更是扑朔迷离,我将在《理解Javascript_09_Function与Object》中解释这一切。
最后的声明:理论过于复杂,我不改保证其正确性。但经过多方的测试,还未发现理论与实际冲突的地方。

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



In C++, function pointers can be converted into function objects through the std::function template: use std::function to wrap function pointers into function objects. Use the std::function::target member function to convert a function object to a function pointer. This transformation is useful in scenarios such as event handling, function callbacks, and generic algorithms, providing greater flexibility and code reusability.

Using STL function objects can improve reusability and includes the following steps: Define the function object interface (create a class and inherit from std::unary_function or std::binary_function) Overload operator() to define the function behavior in the overloaded operator() Implement the required functionality using function objects via STL algorithms (such as std::transform)

The functions of function objects in STL mainly include: 1. Container comparison and sorting (such as std::sort, std::find_if); 2. Algorithm customization (customizing algorithm behavior through custom predicates or comparison functions); 3. Containers Adapters (extend container functionality). In addition, function objects are used in function libraries, object-oriented programming, and parallel programming.

Function pointers and function objects are both mechanisms for handling functions as data. A function pointer is a pointer to a function, while a function object is an object containing an overloaded operator(). Both can capture variables and create closures. The difference is that function pointers are primitive types, while function objects are classes; function pointers must point to valid functions, otherwise undefined behavior will occur, while function objects can exist independently of the function they are created from; function objects are generally easier to retrieve than function pointers use. In practical scenarios, they can be used to specify sorting rules in sorting algorithms.

STL function objects in C++ provide an efficient and flexible way to handle container data, including unary function objects (accepting 1 parameter and returning a result), binary function objects (accepting 2 parameters and returning a result) and functors (Overloaded function call operator). Function objects have the advantages of reusability, scalability, and performance optimization. In the actual case, the std::transform() function uses the std::negate function object to negate each element in the container. Tips include using inline function objects, creating custom lambda expressions, using function objects as return values, and understanding the semantics and limitations of function objects.

C++ function pointers and function objects are tools for dealing with functions. Function pointers store function addresses, and function objects allow overloading operators and maintaining state. They have applications in sorting algorithms, event handling, and strategy patterns, improving code flexibility, reusability, and maintainability.

C++STL provides a variety of function objects that can be used to compare, sort and operate elements. Common function objects include less for ascending sorting, greater for descending sorting, equal_to for comparing equality, and bind2nd and mem_fn for binding function parameters. In practice, the array can be sorted in descending order by using the greater function object, as shown below: Using the sort() function, the greater function object will sort the elements in the specified range in descending order.

C++ function pointers point to functions, allowing functions to be called through pointers. A function object is a class or structure that overloads the operator() operator and can be called like a function. They are useful when working with callback functions, which are functions that are passed to another function as arguments.
