有没有办法判断JavaScript中的函数是否已被覆盖? [英] Is there a way to tell if a function in JavaScript is getting over written?

查看:110
本文介绍了有没有办法判断JavaScript中的函数是否已被覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有办法判断我编写的函数是否被覆盖或重新定义?例如,假设一个名叫杰克的家伙具有以下功能:

Is there a way to tell if a function I've written is getting overwritten or redefined? For example, say a guy named Jack has the function below:

// Jack's function on team 1
function doSomething() {
    // doing things
}

一天,他发现Zaxxon星球北极的一个队友使用了相同的功能名称:

and one day he finds out that a teammate in the north pole of planet Zaxxon has used the same function name:

// Zaxar's function on team 2
function doSomething() {
    // doing something else
}

您可以看到Zaxar和Jack使用相同的函数名称. Zaxar和John都没有发现问题,直到项目进行了6个月.他们知道他们可以在函数中放置警报并在运行时进行检查,但是假设Zaxar和Jack都有2万亿个函数,并且正在使用100万个JavaScript库,并且意识到在每个函数中放置警报是不切实际的.

As you can see Zaxar and Jack are using the same function name. Neither Zaxar nor John discovered the problem until 6 months into the project. They know they can put an alert in their function and check it at runtime but let's say Zaxar and Jack both have 2 trillion functions and they are using 1 million JavaScript libraries and they realize it's impractical to put alerts in each function.

有没有一种方法可以帮助Zaxar和Jack在运行时发现名称冲突?还是有一个设置可以告诉JavaScript引擎在名称冲突时引发错误?

Is there a way to help Zaxar and Jack discover if there are any name collisions at runtime? Or is there a setting that will tell the JavaScript engine to throw errors on name collisions?

更多信息
我正在加载一些库,我认为其中一个正在使用与我相同的函数名.因此,在此示例中,确实没有可以与之关联的其他开发人员.它可以确定我加载的库是否使用相同的函数名,并防止类似的事情发生.

More information
I'm loading in a few libraries and I think that one of them is using the same function name as me. So in this example, there really isn't another developer I can correlate with. It's finding out if the library(s) that I load in are using the same function names and preventing something like this from happening down the line.

推荐答案

尽管有关命名空间的建议是恰当的,但您可能也对writable和configurable属性感兴趣如果您想防止覆盖特定功能,请访问developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty"rel =" nofollow> Object.defineProperty 描述符:

Although the suggestion on namespacing is apt, you may also be interested in the writable and configurable properties of Object.defineProperty descriptors if you wanted to prevent overwriting of specific functions:

Object.defineProperty(window, "myUnchangeableFunction", {value: function () {
    alert("Can't change me");
}, writable: false, configurable: false});

/*
function myUnchangeableFunction () { // TypeError: can't redefine non-configurable property 'myUnchangeableFunction'
    alert('change?');
}

var myUnchangeableFunction = function () { // TypeError: can't redefine non-configurable property 'myUnchangeableFunction'
    alert('change?');
};
*/

myUnchangeableFunction();

但是,由于某些较旧的浏览器可能不支持此功能,因此应在希望支持的浏览器中检查支持.

You should check support in the browsers you wish to support, however, as some older browsers might not support this functionality.

这篇关于有没有办法判断JavaScript中的函数是否已被覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆