Skip to main content
deleted 12 characters in body
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k

Use arrow functions

ECMAScript 6 introduced arrow functions, which can be thought of as lambda functions. They don't have their own this binding. Instead, this is looked up in scope just like a normal variable. That means you don't have to call .bind. That's not the only special behavior they have, please refer to the MDN documentation for more information.

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', () => alert(this.data));
}

Don't use this

ECMAScript 6: Use arrow functions

ECMAScript 6 introduces arrow functions, which can be thought of as lambda functions. They don't have their own this binding. Instead, this is looked up in scope just like a normal variable. That means you don't have to call .bind. That's not the only special behavior they have, please refer to the MDN documentation for more information.

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', () => alert(this.data));
}

Don't use this

ECMAScript 6: Use arrow functions

ECMAScript 6 introduces arrow functions, which can be thought of as lambda functions. They don't have their own this binding. Instead, this is looked up in scope just like a normal variable. That means you don't have to call .bind. That's not the only special behavior they have, please refer to the MDN documentation for more information.

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', () => alert(this.data));
}

Use arrow functions

ECMAScript 6 introduced arrow functions, which can be thought of as lambda functions. They don't have their own this binding. Instead, this is looked up in scope just like a normal variable. That means you don't have to call .bind. That's not the only special behavior they have, please refer to the MDN documentation for more information.

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', () => alert(this.data));
}

Don't use this

edited body
Source Link
kgangadhar
  • 5k
  • 5
  • 38
  • 56

Every function has the method .bind [docs], which returns a new function with this bound to a value. The function has exactly the same behaviourbehavior as the one you called .bind on, only that this was set by you. No matter how or when that function is called, this will always refer to the passed value.

Note: When a binding context for jQuery, use jQuery.proxy [docs] instead. The reason to do this is so that you don't need to store the reference to the function when unbinding an event callback. jQuery handles that internally.

ECMAScript 6 introduces arrow functions, which can be thought of as lambda functions. They don't have their own this binding. Instead, this is looked up in scope just like a normal variable. That means you don't have to call .bind. That's not the only special behaviourbehavior they have, please refer to the MDN documentation for more information.

Every function has the method .bind [docs], which returns a new function with this bound to a value. The function has exactly the same behaviour as the one you called .bind on, only that this was set by you. No matter how or when that function is called, this will always refer to the passed value.

Note: When binding context for jQuery, use jQuery.proxy [docs] instead. The reason to do this is so that you don't need to store the reference to the function when unbinding an event callback. jQuery handles that internally.

ECMAScript 6 introduces arrow functions, which can be thought of as lambda functions. They don't have their own this binding. Instead, this is looked up in scope just like a normal variable. That means you don't have to call .bind. That's not the only special behaviour they have, please refer to the MDN documentation for more information.

Every function has the method .bind [docs], which returns a new function with this bound to a value. The function has exactly the same behavior as the one you called .bind on, only that this was set by you. No matter how or when that function is called, this will always refer to the passed value.

Note: When a binding context for jQuery, use jQuery.proxy [docs] instead. The reason to do this is so that you don't need to store the reference to the function when unbinding an event callback. jQuery handles that internally.

ECMAScript 6 introduces arrow functions, which can be thought of as lambda functions. They don't have their own this binding. Instead, this is looked up in scope just like a normal variable. That means you don't have to call .bind. That's not the only special behavior they have, please refer to the MDN documentation for more information.

added 39 characters in body
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k

this (aka "the context") is a special keyword inside each function and its value only depends on how the function was called, not how/when/where it was defined. It is not affected by lexical scopes, like other variables (except for arrow functions, see below). Here are some examples:

this (aka "the context") is a special keyword inside each function and its value only depends on how the function was called, not how/when/where it was defined. It is not affected by lexical scopes, like other variables. Here are some examples:

this (aka "the context") is a special keyword inside each function and its value only depends on how the function was called, not how/when/where it was defined. It is not affected by lexical scopes like other variables (except for arrow functions, see below). Here are some examples:

Corrected spelling (was "has", should be "as") in this sentence: [...] by using an anonymous function -> as <- callback / event handler and assign the object (this) to another variable [...]
Source Link
Loading
corrected the code...
Source Link
mido
  • 24.8k
  • 15
  • 97
  • 119
Loading
added 89 characters in body
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading
added 81 characters in body
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading
fix typo
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading
added 353 characters in body
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading
added 35 characters in body
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading
added information about arrow functions
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading
added 1681 characters in body
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading
added 1681 characters in body
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading
edited body
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading
added 2 characters in body
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading
Source Link
Felix Kling
  • 810.6k
  • 176
  • 1.1k
  • 1.2k
Loading