除了无法找到设置“ this”变量的好方法之外,我对Javascript有很好的理解。考虑:
var myFunction = function(){
alert(this.foo_variable);
}
var someObj = document.body; //using body as example object
someObj.foo_variable = "hi"; //set foo_variable so it alerts
var old_fn = someObj.fn; //store old value
someObj.fn = myFunction; //bind to someObj so "this" keyword works
someObj.fn();
someObj.fn = old_fn; //restore old value
没有最后四行,有没有办法做到这一点?这很烦人……我试图绑定一个匿名函数,我认为它是美丽而聪明的,但无济于事:
var myFunction = function(){
alert(this.foo_variable);
}
var someObj = document.body; //using body as example object
someObj.foo_variable = "hi"; //set foo_variable so it alerts
someObj.(function(){ fn(); })(); //fail.
显然,将变量传递到myFunction是一个选项……但这不是这个问题的重点。
谢谢。
为JavaScript中的所有函数定义了两种方法call()
,和apply()
。函数语法如下:
call( /* object */, /* arguments... */ );
apply(/* object */, /* arguments[] */);
这些函数的作用是调用它们所调用的函数,并将object参数的值分配给this。
var myFunction = function(){
alert(this.foo_variable);
}
myFunction.call( document.body );
我认为您正在寻找call
:
myFunction.call(obj, arg1, arg2, ...);
这就要求myFunction
与this
设置为obj
。
还有一种稍微不同的方法apply
,该方法将函数参数作为数组:
myFunction.apply(obj, [arg1, arg2, ...]);
如果您想将该this
值“存储”到一个函数中,以便以后可以无缝调用它(例如,当您再无权访问该值时),则可以bind
(尽管并非在所有浏览器中都可用):
var bound = func.bind(someThisValue);
// ... later on, where someThisValue is not available anymore
bound(); // will call with someThisValue as 'this'
我对如何绑定的搜索this
使我来到这里,所以我发表了自己的发现:在“ ECMAScript 2015”中,我们还可以使用箭头功能来按词法设置此功能。
请参阅:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions
代替:
function Person() {
setInterval(function growUp() {
// The callback refers to the `self` variable of which
// the value is the expected object.
this.age++;
}.bind(this), 1000);
}
我们现在可以做:
function Person(){
this.age = 0;
setInterval(() => {
this.age++; // |this| properly refers to the person object
}, 1000);
}
var p = new Person();
this
在javascript中设置关键字。
Javascript具有3种内置方法,可this
方便地设置关键字。它们都位于Function.prototype
对象上,因此每个函数都可以使用它们(因为每个函数都是通过原型继承从该原型继承的)。这些功能如下:
Function.prototype.call()
:此函数将要this
用作第一个参数的对象用作对象。那么其余的参数就是所调用函数的各个参数。Function.prototype.apply()
:此函数将要this
用作第一个参数的对象用作对象。然后第二个参数是一个数组,其中包含被调用的函数的参数值(数组的第一个元素是函数的第一个参数,数组的第二个参数是函数的第二个参数等)。Function.prototype.bind()
:此函数返回一个新函数,其值不同this
。它this
会将要设置为值的对象作为第一个参数,然后返回一个新的函数对象。
调用/应用和绑定之间的区别:
call
并且apply
在它们立即调用函数(具有的预定义值this
)方面相似bind
call
与apply
以下事实不同:事实上,此函数返回具有不同this
值绑定的新函数。
例子:
const thisObj = {
prop1: 1,
prop2: 2,
};
function myFunc(arg1, arg2) {
console.log(this.prop1, this.prop2);
console.log(arg1, arg2);
}
// first arg this obj, other arguments are the
// respective arguments of the function
myFunc.call(thisObj, 'Call_arg1', 'Call_arg2');
// first arg this obj, other argument is an array which
// are the respective arguments of the function
myFunc.apply(thisObj, ['Apply_arg1', 'Apply_arg2']);
// the bind method returns a new function with a different
// this context which is stored in the newMyFunc variable
const newMyFunc = myFunc.bind(thisObj);
// now we can call the function like a normal function
newMyFunc('first', 'second');
本文地址:http://javascript.askforanswer.com/qingsongshezhicibianliang.html
文章标签:javascript , scope , this , variables
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
文章标签:javascript , scope , this , variables
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!