简体   繁体   English

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

[英]Is it possible to check whether a function is a javascript function or a developer defined function?

Given a function, I'd like to know whether it's a developer-defined function or a built-in function provided by JavaScript engine. 给定一个函数,我想知道它是开发人员定义的函数还是JavaScript引擎提供的内置函数。 Is it possible? 可能吗?

For a developer-defined function, I'd like to trace its execution. 对于开发人员定义的函数,我想跟踪其执行情况。

I've got a solution by myself---using the valueOf method(). 我自己有一个解决方案-使用valueOf method()。

Copied and pasted the following : 复制并粘贴以下内容

The valueOf method returns a string which represents the source code of a function. valueOf方法返回一个表示函数源代码的字符串。 This overrides the Object.valueOf method. 这将覆盖Object.valueOf方法。 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: JavaScript通常在后台调用valueOf方法,但是为了演示输出,以下代码首先创建一个名为Car的Function对象,然后显示该函数的值:

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

Output: function car(make, model, year) {this.make = make, this.model = model, this.year = year 输出:function car(make,model,year){this.make =品牌,this.model =型号,this.year =年

With the built-in Function object the valueOf method would produce the following string: 使用内置的Function对象,valueOf方法将产生以下字符串:

Output: function Function() { [native code] }. 输出:函数Function(){[本地代码]}。

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. Javascript内置对象和函数不具有(或缺少)可以在运行时进行测试的任何特殊属性,以保证它不是开发人员定义的函数。 All methods and objects can be overridden by the developer. 开发人员可以覆盖所有方法和对象。

The valueOf method you mention in your own answer will not work as you mention it. 您在自己的答案中提到的valueOf方法将无法正常使用。

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: Function.prototype没有valueOf方法,它是从Object.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方法混淆了(或者您正在警告valueOf方法调用,这会在大多数浏览器上导致隐式ToString转换)。

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方法,并且在几乎所有的实现中,它都会在函数体中返回一个包含"[native code]"的字符串表示形式,我不建议过多使用它,因为Function.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方法时将返回不同的结果,例如,某些移动浏览器将为任何函数对象返回相同的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM