javascript中的call prototype.call函数
问题是,当我使用test.call()时,它会调用原型的调用实现,但当我使用test()时,它不会调用call()。我希望能够使用test触发prototype.call()。代码如下:javascript中的call prototype.call函数,javascript,oop,Javascript,Oop,问题是,当我使用test.call()时,它会调用原型的调用实现,但当我使用test()时,它不会调用call()。我希望能够使用test触发prototype.call()。代码如下: Function.prototype.call = function () { //do something... return this(); // call original method } func
Function.prototype.call = function () {
//do something...
return this(); // call original method
}
function test() {
alert("test");
}
test.call(); //calls prototype.call()
test(); //doesnt call prototype.call()
为什么希望
test()
调用Function.prototype.call
?它们是不同的功能
每次调用函数时,不会调用要覆盖的本机.call()
方法。只有在您调用它时才会调用它
调用
.call()
会调用test()
,因为这就是它的设计目的。它需要一个函数作为其上下文(this
value),并调用该函数。但这并不意味着.call()
与任何其他函数调用都有任何关系。为什么希望test()
调用function.prototype.call
?它们是不同的功能
每次调用函数时,不会调用要覆盖的本机.call()
方法。只有在您调用它时才会调用它
调用
.call()
会调用test()
,因为这就是它的设计目的。它需要一个函数作为其上下文(this
value),并调用该函数。但这并不意味着.call()
与任何其他函数调用都有任何关系。这里有一个我刚刚提出的解决方案(归功于CliffsofInasenacy,它指出了代码中的一个关键错误并进行了纠正)。它按照调用顺序记录在某一点之后调用的所有非标准函数
// array of all called functions
var calledFunctions = [];
// custom func object that has name of function and
// and the actual function on it.
function func(_func, name) {
return function() {
calledFunctions.push(name)
return _func.apply(this, arguments);
};
}
test = function() {
alert("hi");
}
otherTest = function() {
alert("hello");
}
// put this bit somewhere after you've defined all your functions
// but *before* you've called any of them as all functions called
// after this point are logged. It logs all non-standard functions
// in the order that they are called.
for (prop in window) {
if (window.hasOwnProperty(prop) && typeof window[prop] === 'function' && window[prop].toString().indexOf('[native code]') < 0) {
window[prop] = func(window[prop], prop);
}
}
otherTest();
test();
otherTest();
console.log(calledFunctions);
//所有被调用函数的数组
var calledFunctions=[];
//具有函数名和
//以及它的实际功能。
函数func(_func,名称){
返回函数(){
calledFunctions.push(名称)
return _func.apply(这是参数);
};
}
测试=函数(){
警报(“hi”);
}
otherTest=函数(){
警惕(“你好”);
}
//定义完所有函数后,将此位放在某个位置
//但是*在*之前*你调用了它们中的任何一个,就像调用了所有函数一样
//在这一点之后,将记录。它记录所有非标准函数
//按照它们被称为的顺序。
用于(窗口中的道具){
if(window.hasOwnProperty(prop)和&typeof-window[prop]==“函数”&&window[prop].toString().indexOf(“[native-code]”)小于0){
窗口[prop]=func(窗口[prop],prop);
}
}
其他测试();
test();
其他测试();
console.log(调用函数);
可以找到正在工作的演示。这里有一个我刚刚想出的解决方案(归功于CliffsofInasenacy,它指出了代码中的一个关键错误并进行了更正)。它按照调用顺序记录在某一点之后调用的所有非标准函数
// array of all called functions
var calledFunctions = [];
// custom func object that has name of function and
// and the actual function on it.
function func(_func, name) {
return function() {
calledFunctions.push(name)
return _func.apply(this, arguments);
};
}
test = function() {
alert("hi");
}
otherTest = function() {
alert("hello");
}
// put this bit somewhere after you've defined all your functions
// but *before* you've called any of them as all functions called
// after this point are logged. It logs all non-standard functions
// in the order that they are called.
for (prop in window) {
if (window.hasOwnProperty(prop) && typeof window[prop] === 'function' && window[prop].toString().indexOf('[native code]') < 0) {
window[prop] = func(window[prop], prop);
}
}
otherTest();
test();
otherTest();
console.log(calledFunctions);
//所有被调用函数的数组
var calledFunctions=[];
//具有函数名和
//以及它的实际功能。
函数func(_func,名称){
返回函数(){
calledFunctions.push(名称)
return _func.apply(这是参数);
};
}
测试=函数(){
警报(“hi”);
}
otherTest=函数(){
警惕(“你好”);
}
//定义完所有函数后,将此位放在某个位置
//但是*在*之前*你调用了它们中的任何一个,就像调用了所有函数一样
//在这一点之后,将记录。它记录所有非标准函数
//按照它们被称为的顺序。
用于(窗口中的道具){
if(window.hasOwnProperty(prop)和&typeof-window[prop]==“函数”&&window[prop].toString().indexOf(“[native-code]”)小于0){
窗口[prop]=func(窗口[prop],prop);
}
}
其他测试();
test();
其他测试();
console.log(调用函数);
可以找到工作演示。感谢您的澄清。那么,如何才能做到这一点呢。每当调用任何其他函数时,我都需要调用一个函数。这根本不可能。你为什么认为你需要这个?@AliTarhini:不是自动的。您可以手动修饰函数,但需要预先了解这些函数。我需要创建一个所有正在执行的函数的日志听起来像是调试器的工作。感谢您的澄清。那么,如何才能做到这一点呢。每当调用任何其他函数时,我都需要调用一个函数。这根本不可能。你为什么认为你需要这个?@AliTarhini:不是自动的。您可以手动修饰函数,但需要预先了解这些函数。我需要创建所有正在执行的函数的日志听起来像是调试器的工作。请记住,传递给函数的任何参数以及返回值都将丢失,并且函数的上下文可能会被更改。您应该执行
返回该操作。_func.apply(这是参数)代码>。而且,您可以在没有构造函数的情况下真正做到这一点。您必须关闭that
以引用\u func
和name
作为属性,因此您可以跳过that
对象,只需关闭参数即可。非常有趣,谢谢你指出这一点!我已经编辑了我的答案,并给了你荣誉。只是想知道这意味着什么:window[prop].toString().indexOf(“[native code]”)0@AliTarhini:它将窗口的属性转换为字符串表示形式,然后检查是否找到字符串“[本机代码]”
。如果是,则属性是Javascript的“本机”或标准。谢谢。上面的代码工作得很好。是否可以获取未嵌入函数中的脚本,例如直接写入脚本标记中的内联脚本?请记住,传递给函数的任何参数以及返回值都将丢失,并且函数的上下文可能会更改。您应该执行返回该操作。_func.apply(这是参数)代码>。而且,您可以在没有构造函数的情况下真正做到这一点。您必须关闭该
以引用