本文介绍了从内部引用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这段代码

  var crazy = function(){
console.log(this);
console.log(this.isCrazy); //错了。
}
crazy.isCrazy ='完全';
crazy();
// ouput =>
// DOMWindow
// undefined

从疯狂()里面这个'指的是窗口,我想这是有意义的,因为通常你想要这个引用函数所附加的对象,但是如何让函数引用自身,并访问自身的属性集? / p>

答案:



不要使用arguments.callee,只需使用命名函数。



注意:你应该避免使用arguments.callee(),只需给每个函数(表达式)命名。通过

解决方案

我认为你要求arguments.callee,但现在已弃用



  var crazy = function(){ 
console.log(this);
console.log(arguments.callee.isCrazy); // 对。
}
crazy.isCrazy ='完全';
crazy();
// ouput =>
// DOMWindow
//总计


Consider this piece of code

var crazy = function() {
    console.log(this);
    console.log(this.isCrazy); // wrong.
}
crazy.isCrazy = 'totally';
crazy();
// ouput =>
// DOMWindow
// undefined

From inside crazy() 'this' refers to the window, which I guess makes sense because normally you'd want this to refer to the object the function is attached to, but how can I get the function to refer to itself, and access a property set on itself?

Answer:

Don't use arguments.callee, just use a named function.

"Note: You should avoid using arguments.callee() and just give every function (expression) a name." via MDN article on arguments.callee

解决方案

I think you are asking for arguments.callee, but it's deprecated now.

https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments/callee

var crazy = function() {
    console.log(this);
    console.log(arguments.callee.isCrazy); // right.
}
crazy.isCrazy = 'totally';
crazy();
// ouput =>
// DOMWindow
// totally

这篇关于从内部引用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 13:26