JavaScript - 获取调用对象 [英] JavaScript - Get calling object

查看:56
本文介绍了JavaScript - 获取调用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Javascript如何找到来电者功能?

大家好!

有没有办法从函数中获取这个的值已经调用了当前的功能?
看看这个:

Is there a way to get the value of this from the function which has called the current function? Look at this:

function TraceMySelf(){
    console.log(this);
}
function A(){
    TraceMySelf();
    console.log(this);
}

var a = new A();

执行此代码时,控制台首先显示窗口 object,然后是 a 对象。如何使代码显示 a 对象两次,只更改第2行?我知道我可以在 A 中使用这个来应用函数,但这不是我想要的。

When this code is executed, the console displays first the window object and then the a object. How can I make the code display the a object twice, with only changing line 2? I know that I could apply the function inside A with this, but that isn't what I want.

这可能吗?

感谢您的帮助!

推荐答案

我认为这是你问题的答案: StackOverflow 280389

I think this is the answer to your question: StackOverflow 280389

但是,我认为正确的答案是不要这样做。我认为这与JavaScript的设计方式背道而驰。

However, I think the right answer is "don't do that". I think it runs counter to how JavaScript is designed.

也许值得一看 jQuery Proxy ,用于链接函数和对象的另一种方式。

It might also be worth looking at jQuery Proxy for another way of linking function and object.

这篇关于JavaScript - 获取调用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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