如何在javascript中更改函数的上下文

如何在javascript中更改函数的上下文,javascript,function,scope,Javascript,Function,Scope,我试图理解为什么在javascript中,您可能希望更改函数的上下文。我正在寻找一个真实世界的例子或东西,它将帮助我理解如何/为什么使用这种技术,以及它的意义是什么 使用此示例说明了该技术(来自) jQuery很好地利用了它: $('a').each(function() { // "this" is an a element - very useful }); 实际的jQuery代码如下所示: for ( name in object ) { if ( callback.ca

我试图理解为什么在javascript中,您可能希望更改函数的上下文。我正在寻找一个真实世界的例子或东西,它将帮助我理解如何/为什么使用这种技术,以及它的意义是什么

使用此示例说明了该技术(来自)


jQuery很好地利用了它:

$('a').each(function() {
    // "this" is an a element - very useful
});
实际的jQuery代码如下所示:

for ( name in object ) {
    if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
        break;
    }
}

如果它只是执行了
回调(name,object[name])
,那么
这个
就不会被设置为迭代器中的当前对象,而必须使用该参数。基本上,它只是让事情变得更简单。

请看一下这个例子:

<script>
var el = document.getElementById('button');
el.onclick = function(){
    this.value = "Press Me Again"; //this --> now refers to the the element button not on the window
}

//Another Example:
var Person = function(name,location){
  this.name = name;
  this.location = location;  
  alert(this.location); 
}   
var p2 = new Person("Samantha","California"); //this refers to the instance of the function Person(Person now acts as a class)
var p1 = Person(); // this refers to the window(Person simply acts as a simple function)
</script>
<button id="button1">Press Me</button>

var el=document.getElementById('button');
el.onclick=函数(){
this.value=“再次按我”;//此-->现在指的是不在窗口上的元素按钮
}
//另一个例子:
var Person=功能(名称、位置){
this.name=名称;
这个位置=位置;
警报(此位置);
}   
变量p2=新人(“萨曼莎”、“加利福尼亚”)//这是指函数Person的实例(Person现在充当一个类)
变量p1=Person();//这是指窗口(Person只是作为一个简单的函数)
按我

new关键字会更改上下文。

从AJAX请求执行回调时,它非常有用:

function Person(_id, _name) {
    this.id = _id;
    this.name = _name;
};

Person.prototype.sayHi = function(greeting) {
    alert(greeting + " from " + this.name);
};

Person.prototype.loadFromAJAX = function(callback) {
    // in this example, it's jQuery, but could be anything
    var t = this;
    $.get("myurl.php", function(data) {
        callback.call(t, data.greeting);
    });
};
事实上,这是一个相当糟糕的例子

它在jQuery中有大量的用途。例如,jQuery().get()函数:

get: function( num ) {
    return num === undefined ?
        // Return a 'clean' array
        Array.prototype.slice.call( this ) :
        // Return just the object
        this[ num ];
}

它使用数组原型的函数,但在jQuery对象的上下文中。

我遇到的一个真实示例:

如果将函数作为事件处理程序添加到DOM元素中,并且在该函数中使用“this”,则“this”将引用添加事件处理程序的DOM元素

但是该函数可能是一个对象的方法,您希望其中使用的“this”关键字引用所有者对象……因此您需要更改上下文,以便“this”不会引用DOM元素,而是引用所有者对象

您可以使用proxy()函数在jquery中轻松更改函数的上下文。 见这个问题:
第一个答案

当使用
setTimeout
时,我总是需要不同的上下文,而jQuery有一个方便的函数,可以实现以下功能:

function iAmCalledAfterTimeout()
{
     alert(this.myProperty); //it will alert "hello world"
}    

setTimeout($.proxy(iAmCalledAfterTimeout, {myProperty:"hello world"}), 1000);

bind函数可能就是您要查找的函数,bind函数在传入的上下文中返回一个新函数,实际情况可能是使用jquery委托将某些行为附加到dom元素,并且希望在不同的上下文中执行回调。”因为jquery delgate中的默认上下文是绑定到处理程序的dom对象,这意味着您不能访问除属于该dom对象的属性之外的任何属性

;我找到此页面是因为我需要为正在拨打的电话设置上下文,但无法记住语法…:)+1的标题应该是:“如何在javascript中更改函数的上下文”(jk-感谢您发布此内容!)+1我正在寻找如何在类中保持此一致性…只是为了澄清一下,
.call()
的第一个参数变成了
this
。更多细节@
function iAmCalledAfterTimeout()
{
     alert(this.myProperty); //it will alert "hello world"
}    

setTimeout($.proxy(iAmCalledAfterTimeout, {myProperty:"hello world"}), 1000);