Show inner this 在这种情况下,内部函数的 this 指向globalThis 对象(即非严格模式下,调用的函数未设置 this 时指向的默认对象)。 类中的绑定方法 和其他普通函数一样,方法中的 this 值取决于它们如何被调用。有时,改写这个行为,让类中的 this 值总是指向这个类实例会很有用。为了做到这一点,可在构造函数中...
1varname = "Bob";2varnameObj ={3name : "Tom",4showName :function(){5alert(this.name);6},7waitShowName :function(){8setTimeout(this.showName, 1000);9}10};1112nameObj.waitShowName(); 要解决这个问题我们需要了解Javascript的this关键字的用法。 this指向哪里? 一般而言,在Javascript中,this...
What the this-keyword allows us to do, is it allows us to reuse functions with different contexts, or in other words it allows us to decide which objects should be focal when invoking a function or a methods. Imagine we had one function, and we had a bunch of objects that had similar...
The 'this' keyword can lead to a lot of confusion in JS. So can function calls without parenthesis. What's the idea behind this.someFunction.bind(this)?
Learn how the JavaScript “this” keyword behaves inside of function declarations and expressions, even when nested 10 levels deep. In the article: The JavaScript “this” Keyword Deep Dive: An Overview, we discussed a number of scenarios in which the JavaScript “this” Keyword has different me...
「The this keyword evaluates to the value of the ThisBinding of the current execution context.」「this 这个关键字代表的值为当前执行上下文的ThisBinding。」然后再来看看MDN 对this 的定义:「In most cases, the value of this is determined by how a function is called.」「在大多数的情况下,this ...
function bar() { alert(this); } bar(); // global - 因为bar方法被调用时属于 global 对象 var foo = { baz: function() { alert(this); } } foo.baz(); // foo - 因为baz()方法被调用时术语foo对象 如果this就这么简单,那上面的代码就足够了。我们可以进一步使事情变得复杂,通过不同的调用语法...
In JavaScript,thiskeyword refers to theobjectwhere it is called. 1. this Inside Global Scope Whenthisis used alone,thisrefers to the global object (windowobject in browsers). For example, leta =this;console.log(a);// Window {}this.name ='Sarah';console.log(window.name);// Sarah ...
thethiskeyword refers to anobject. 但究竟指向哪个object要看this在哪里被调用或者如何被调用 在对象方法object中,指向对象 单独调用时,指向global 在函数function中,严格模式下为undefined,非严格模式下指向global 在事件event中,指向接受该事件的元素(比如一个监听事件addEventListener,指向绑定该事件的元素) ...
fullName :function() { returnthis.firstName+" "+this.lastName; } }; Try it Yourself » What isthis? In JavaScript, thethiskeyword refers to anobject. Thethiskeyword refers todifferent objectsdepending on how it is used: In an object method,thisrefers to theobject. ...