大部分函数并不接受this参数,所以最好还是采用上文中的其它方式来绑定this。 译文参考于6 Ways to Bind JavaScript‘s this Keyword in React, ES6 & ES7
console.log(func()); 阮一峰博客:this就是函数运行时所在的环境对象,this的指向在函数创建的时候是决定不了的,在调用的时候才能决定(这里es6的箭头函数是个例外),http://www.ruanyifeng.com/blog/2010/04/using_this_keyword_in_javascript.html 上面的代码在浏览器和node中,都会输出 1和 undefined,体会下. 我...
箭头函数是在 ES6 引入的,相对于函数表达式来说是一种更简洁的方式。 箭头函数名称的来源是因为使用了=>。 语法 代码语言:javascript 代码运行次数:0 运行 AI代码解释 constfunctionName=(arg1,arg2,...argN)=>{returnvalue;} 例子 代码语言:javascript ...
「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.」 「在大多数的情况下,t...
在很多编程语言中都有this这个特殊关键字的存在,比如Java中的this,还有本文要说到的JavaScript中的this。那么,JavaScript中this究竟有什么特性和用法呢?它又是如何定义的呢? 先来看看ECMAScript 标准规范对this 的定义: 「The this keyword evaluates to the value of the ThisBinding of the current execution contex...
也许你在其他面向对象的编程语言曾经看过this,也知道它会指向某个构造器(constructor)所建立的对象。但事实上在JavaScript里面,this所代表的不仅仅是那个被建立的对象。先来看看ECMAScript 标准规范对this 的定义:「The this keyword evaluates to the value of the ThisBinding of the current execution context.」...
thethiskeyword refers to anobject. 但究竟指向哪个object要看this在哪里被调用或者如何被调用 在对象方法object中,指向对象 单独调用时,指向global 在函数function中,严格模式下为undefined,非严格模式下指向global 在事件event中,指向接受该事件的元素(比如一个监听事件addEventListener,指向绑定该事件的元素) ...
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 ...
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)?
http://www.cnblogs.com/justany/archive/2012/11/01/the_keyword_this_in_javascript.html this是Javascript语言的一个关键字它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用,下面分四种情况,详细讨论this的用法 this是Javascript语言的一个关键字。