Javascript 将变量设置为';这个';?
例如,有时当我看别人的代码时,他们会去Javascript 将变量设置为';这个';?,javascript,this,Javascript,This,例如,有时当我看别人的代码时,他们会去var self=this
var self=this例如,在jquery中,govar$self=$(this)代码>
这样做有什么特别的原因吗?它保留了this
的值,以便在当前函数中定义的函数中使用
// Contrived example
var myObject = {
func: function () {
var self = this;
setTimeout(bar, 1000);
function bar () {
alert(this); // `window`
alert(self); // `myObject`
}
}
};
myObject.func();
它保留this
的值,以便在当前函数中定义的函数中使用
// Contrived example
var myObject = {
func: function () {
var self = this;
setTimeout(bar, 1000);
function bar () {
alert(this); // `window`
alert(self); // `myObject`
}
}
};
myObject.func();
通过在某些上下文中保持对this
的引用,您可以在其他上下文中访问它,例如在成员函数或forEach
循环中
考虑以下示例:
function ViewModel() {
var self = this;
self.linksArray = ["link1", "link2", "link3"];
self.linksArray.forEach(function(link) {
// this refers to the DOM window
// and self refers to the parent context (ViewModel)
});
};
通过在某些上下文中保持对this
的引用,您可以在其他上下文中访问它,例如在成员函数或forEach
循环中
考虑以下示例:
function ViewModel() {
var self = this;
self.linksArray = ["link1", "link2", "link3"];
self.linksArray.forEach(function(link) {
// this refers to the DOM window
// and self refers to the parent context (ViewModel)
});
};
特定的示例(不使用JQuery)是函数闭包。在函数闭包中引用它是指函数对象,而不是定义闭包的上下文。您的示例是处理关闭问题的一种方法:
var that = this;
function(){
that.something = 1;
}();
处理此问题的另一种方法是对函数使用apply
方法:
function(arg){
this.something = 1;
}.apply(this, argumentArray);
apply
中的第一个参数是“this
参数”,“this
”也将引用该参数 特定示例(不使用JQuery)是函数闭包。在函数闭包中引用它是指函数对象,而不是定义闭包的上下文。您的示例是处理关闭问题的一种方法:
var that = this;
function(){
that.something = 1;
}();
处理此问题的另一种方法是对函数使用apply
方法:
function(arg){
this.something = 1;
}.apply(this, argumentArray);
apply
中的第一个参数是“this
参数”,“this
”也将引用该参数 这样做的一个目的是使内部函数能够访问此
。例如:
function clickHandler(){
console.log(this); // this is body
var $self = this;
function inner(){
console.log(this); // this is window
console.log($self); // this is body
}
inner();
}
$("body").click(clickHandler);
在控制台中运行它以获得一种感觉。这样做的一个目的是使内部函数可以访问
这个。例如:
function clickHandler(){
console.log(this); // this is body
var $self = this;
function inner(){
console.log(this); // this is window
console.log($self); // this is body
}
inner();
}
$("body").click(clickHandler);
在控制台中运行它以获得感觉。正如其他人所提到的,如果希望在其他函数中使用它,可以将变量设置为$(this)
// Contrived example
var myObject = {
func: function () {
var self = this;
setTimeout(bar, 1000);
function bar () {
alert(this); // `window`
alert(self); // `myObject`
}
}
};
myObject.func();
在实际的例子中,当执行与页面上的事件相关联的ajax调用时。使用JQuery:
<script>
$(document).on("click", ".mySelector", function () {
// Where we are in the click event, $(this) refers to whatever
// element has a class of mySelector that was clicked
var self = $(this);
theDiv.html('');
$.ajax({
cache: false,
type: "GET",
url: "/SomeAjaxMethod",
data: { },
success: function (data) {
// Trying to access $(this) here will return undefined, as
// we are technically in the callback method
// Where our event is based on a class, there is likely more
// than one element on the page with the class, so it would be
// difficult to get the exact element again without some other code
self.html(data);
},
error: function (xhr, ajaxOptions, thrownError) {
alert("Ajax failed.")
}
}); // end ajax call
}); // end on mySelector class click
</script>
$(document).on(“click”,“.mySelector”,函数(){
//在点击事件中,$(这个)指的是任何东西
//元素具有已单击的mySelector类
var self=$(这是);
html(“”);
$.ajax({
cache:false,
键入:“获取”,
url:“/SomeAjaxMethod”,
数据:{},
成功:功能(数据){
//在此处尝试访问$(this)将返回undefined,如下所示
//从技术上讲,我们使用的是回调方法
//如果我们的活动是基于一个类,那么可能会有更多
//类的页面上有多个元素,因此
//如果没有其他代码,很难再次获得准确的元素
html(数据);
},
错误:函数(xhr、ajaxOptions、thrownError){
警报(“Ajax失败”)
}
});//结束ajax调用
}); // 在mySelector类上结束单击
或:
$(文档).ready(函数(){
$('.foo')。单击(函数(){
var self=$(this);//使用foo类单击的任何元素
$('.bar')。每个(函数(){
var bar=$(this);//循环中bar元素的当前迭代
var baz=self;//self仍然是初始值,但$(this)不是
});//结束条循环
});//结束foo单击
}); // 结束文件准备就绪
如其他人所述,如果希望在其他函数中使用变量,可以将其设置为$(this)
// Contrived example
var myObject = {
func: function () {
var self = this;
setTimeout(bar, 1000);
function bar () {
alert(this); // `window`
alert(self); // `myObject`
}
}
};
myObject.func();
在实际的例子中,当执行与页面上的事件相关联的ajax调用时。使用JQuery:
<script>
$(document).on("click", ".mySelector", function () {
// Where we are in the click event, $(this) refers to whatever
// element has a class of mySelector that was clicked
var self = $(this);
theDiv.html('');
$.ajax({
cache: false,
type: "GET",
url: "/SomeAjaxMethod",
data: { },
success: function (data) {
// Trying to access $(this) here will return undefined, as
// we are technically in the callback method
// Where our event is based on a class, there is likely more
// than one element on the page with the class, so it would be
// difficult to get the exact element again without some other code
self.html(data);
},
error: function (xhr, ajaxOptions, thrownError) {
alert("Ajax failed.")
}
}); // end ajax call
}); // end on mySelector class click
</script>
$(document).on(“click”,“.mySelector”,函数(){
//在点击事件中,$(这个)指的是任何东西
//元素具有已单击的mySelector类
var self=$(这是);
html(“”);
$.ajax({
cache:false,
键入:“获取”,
url:“/SomeAjaxMethod”,
数据:{},
成功:功能(数据){
//在此处尝试访问$(this)将返回undefined,如下所示
//从技术上讲,我们使用的是回调方法
//如果我们的活动是基于一个类,那么可能会有更多
//类的页面上有多个元素,因此
//如果没有其他代码,很难再次获得准确的元素
html(数据);
},
错误:函数(xhr、ajaxOptions、thrownError){
警报(“Ajax失败”)
}
});//结束ajax调用
}); // 在mySelector类上结束单击
或:
$(文档).ready(函数(){
$('.foo')。单击(函数(){
var self=$(this);//使用foo类单击的任何元素
$('.bar')。每个(函数(){
var bar=$(this);//循环中bar元素的当前迭代
var baz=self;//self仍然是初始值,但$(this)不是
});//结束条循环
});//结束foo单击
}); // 结束文件准备就绪
是,如果您想在另一个函数(闭包)中引用此
的值。看,是的,只要你愿意