是否可以检查函数是javascript函数还是开发人员定义的函数?

时间:2022-11-05 12:16:05

Given a function, I'd like to know whether it's a developer-defined function or a built-in function provided by JavaScript engine. Is it possible?

给定一个函数,我想知道它是由开发人员定义的函数还是由JavaScript引擎提供的内置函数。是可能的吗?

For a developer-defined function, I'd like to trace its execution.

对于一个开发人员定义的函数,我想跟踪它的执行。

3 个解决方案

#1


2  

The valueOf method you mention in your own answer will not work as you mention it.

你在你自己的答案里提到的方法的价值不会像你提到的那样起作用。

The Function.prototype doesn't have a valueOf method, it is inherited from Object.prototype and this method will simply return the same function object where you call it:

这个函数。原型没有方法的价值,它是从对象继承而来的。prototype和这个方法将简单地返回相同的函数对象,您可以在这里调用它:

Function.valueOf() === Function; // true

I think you are confusing it with the toString method (or you are alerting the valueOf method call which causes on most browsers an implicit ToString conversion).

我认为您将它与toString方法混淆了(或者您正在警告在大多数浏览器中导致了隐式toString转换的方法调用的valueOf方法调用)。

However, you can use the toString method directly on function objects, and in almost all implementations, will return you a string representation containing "[native code]" in its function body, I wouldn't recommend it too much because, the Function.prototype.toString method is implementation dependent...

但是,您可以直接在函数对象上使用toString方法,在几乎所有的实现中,都将返回一个包含“[本机代码]”的字符串表示,在它的函数体中,我不会过多地推荐它,因为函数。prototype。toString方法是依赖于实现的…

function isNative(fn) {
  return /native code/.test(fn.toString);
}

isNative(Function); // true
isNative(function () {}); // false

Again I advise you that there are some browsers that will return different results when using the toString method on functions, for example, some Mobile browsers will return the same string for any function object.

我再次提醒您,在使用toString方法时,有些浏览器会返回不同的结果,例如,一些移动浏览器将返回相同的字符串,用于任何函数对象。

#2


3  

I've got a solution by myself---using the valueOf method().

我自己有一个解决方案——使用方法的valueOf方法()。

Copied and pasted the following:

复制粘贴如下:

The valueOf method returns a string which represents the source code of a function. This overrides the Object.valueOf method. The valueOf method is usually called by JavaScript behind the scenes, but to demonstrate the output, the following code first creates a Function object called Car, and then displays the value of that function:

valueOf方法返回一个字符串,该字符串表示函数的源代码。这个覆盖对象。返回对象的值的方法。方法的valueOf方法通常在后台JavaScript调用,但是为了演示输出,下面的代码首先创建一个名为Car的函数对象,然后显示该函数的值:

Code: function car(make, model, year) {this.make = make, this.model = model, this.year = year} document.write(car.valueOf())

代码:功能汽车(make, model, year){这个。使=,。模型=模型。=今年} document . write(car.valueOf())

Output: function car(make, model, year) {this.make = make, this.model = model, this.year = year

输出:功能车(制作,型号,年份){这个。使=,。模型=模型。年=一年

With the built-in Function object the valueOf method would produce the following string:

使用内置函数对象,方法的valueOf方法将产生以下字符串:

Output: function Function() { [native code] }.

输出:函数函数(){[本机代码]}。

#3


2  

In a general sense, no. Javascript built-in objects and functions do not have (or lack) any special property that can be tested at runtime that guarantees it is not a developer-defined function. All methods and objects can be overridden by the developer.

在一般意义上,没有。Javascript内置对象和函数没有(或缺少)任何可以在运行时测试的特殊属性,以保证它不是开发人员定义的函数。所有方法和对象都可以被开发人员覆盖。

#1


2  

The valueOf method you mention in your own answer will not work as you mention it.

你在你自己的答案里提到的方法的价值不会像你提到的那样起作用。

The Function.prototype doesn't have a valueOf method, it is inherited from Object.prototype and this method will simply return the same function object where you call it:

这个函数。原型没有方法的价值,它是从对象继承而来的。prototype和这个方法将简单地返回相同的函数对象,您可以在这里调用它:

Function.valueOf() === Function; // true

I think you are confusing it with the toString method (or you are alerting the valueOf method call which causes on most browsers an implicit ToString conversion).

我认为您将它与toString方法混淆了(或者您正在警告在大多数浏览器中导致了隐式toString转换的方法调用的valueOf方法调用)。

However, you can use the toString method directly on function objects, and in almost all implementations, will return you a string representation containing "[native code]" in its function body, I wouldn't recommend it too much because, the Function.prototype.toString method is implementation dependent...

但是,您可以直接在函数对象上使用toString方法,在几乎所有的实现中,都将返回一个包含“[本机代码]”的字符串表示,在它的函数体中,我不会过多地推荐它,因为函数。prototype。toString方法是依赖于实现的…

function isNative(fn) {
  return /native code/.test(fn.toString);
}

isNative(Function); // true
isNative(function () {}); // false

Again I advise you that there are some browsers that will return different results when using the toString method on functions, for example, some Mobile browsers will return the same string for any function object.

我再次提醒您,在使用toString方法时,有些浏览器会返回不同的结果,例如,一些移动浏览器将返回相同的字符串,用于任何函数对象。

#2


3  

I've got a solution by myself---using the valueOf method().

我自己有一个解决方案——使用方法的valueOf方法()。

Copied and pasted the following:

复制粘贴如下:

The valueOf method returns a string which represents the source code of a function. This overrides the Object.valueOf method. The valueOf method is usually called by JavaScript behind the scenes, but to demonstrate the output, the following code first creates a Function object called Car, and then displays the value of that function:

valueOf方法返回一个字符串,该字符串表示函数的源代码。这个覆盖对象。返回对象的值的方法。方法的valueOf方法通常在后台JavaScript调用,但是为了演示输出,下面的代码首先创建一个名为Car的函数对象,然后显示该函数的值:

Code: function car(make, model, year) {this.make = make, this.model = model, this.year = year} document.write(car.valueOf())

代码:功能汽车(make, model, year){这个。使=,。模型=模型。=今年} document . write(car.valueOf())

Output: function car(make, model, year) {this.make = make, this.model = model, this.year = year

输出:功能车(制作,型号,年份){这个。使=,。模型=模型。年=一年

With the built-in Function object the valueOf method would produce the following string:

使用内置函数对象,方法的valueOf方法将产生以下字符串:

Output: function Function() { [native code] }.

输出:函数函数(){[本机代码]}。

#3


2  

In a general sense, no. Javascript built-in objects and functions do not have (or lack) any special property that can be tested at runtime that guarantees it is not a developer-defined function. All methods and objects can be overridden by the developer.

在一般意义上,没有。Javascript内置对象和函数没有(或缺少)任何可以在运行时测试的特殊属性,以保证它不是开发人员定义的函数。所有方法和对象都可以被开发人员覆盖。