Can't arrow functions be used as setters [duplicate] - javascript

In ES6, both of these are legal:
var chopper = {
owner: 'Zed',
getOwner: function() { return this.owner; }
};
and, as shorthand:
var chopper = {
owner: 'Zed',
getOwner() { return this.owner; }
}
Is it possible to use the new arrow functions as well? In trying something like
var chopper = {
owner: 'John',
getOwner: () => { return this.owner; }
};
or
var chopper = {
owner: 'John',
getOwner: () => (this.owner)
};
I get an error message suggesting that the method does not have access to this. Is this just a syntax issue, or can you not use fat-arrow methods inside of ES6 objects?

Arrow functions are not designed to be used in every situation merely as a shorter version of old-fashioned functions. They are not intended to replace function syntax using the function keyword. The most common use case for arrow functions is as short "lambdas" which do not redefine this, often used when passing a function as a callback to some function.
Arrow functions cannot be used to write object methods because, as you have found, since arrow functions close over the this of the lexically enclosing context, the this within the arrow is the one that was current where you defined the object. Which is to say:
// Whatever `this` is here...
var chopper = {
owner: 'Zed',
getOwner: () => {
return this.owner; // ...is what `this` is here.
}
};
In your case, wanting to write a method on an object, you should simply use traditional function syntax, or the method syntax introduced in ES6:
var chopper = {
owner: 'Zed',
getOwner: function() {
return this.owner;
}
};
// or
var chopper = {
owner: 'Zed',
getOwner() {
return this.owner;
}
};
(There are small differences between them, but they're only important if you use super in getOwner, which you aren't, or if you copy getOwner to another object.)
There was some debate on the es6 mailing list about a twist on arrow functions which have similar syntax but with their own this. However, this proposal was poorly received because that is mere syntax sugar, allowing people to save typing a few characters, and provides no new functionality over existing function syntax. See the topic unbound arrow functions.

In this line getOwner: () => this.owner should be:
var chopper = {
owner: 'John',
getOwner: () => this.owner
}; //here `this` refers to `window` object.
console.log(chopper.getOwner());
You would have to declare this into a function:
var chopper = {
owner: 'John',
getOwner() { return this.owner }
};
console.log(chopper.getOwner());
Or:
var chopperFn = function(){
this.setOwner = (name) => this.owner = name;
Object.assign(this,{
owner: 'Jhon',
getOwner: () => this.owner,
})
}
var chopper = new chopperFn();
console.log(chopper.getOwner());
chopper.setOwner('Spiderman');
console.log(chopper.getOwner());

If you have to use arrow function, you can change this to chopper,
var chopper = {
owner: "John",
getOwner: () => chopper.owner
};
Although this is not best practice, when you change the object name, you have to change this arrow function.

A quick tip that I follow to use arrow functions.
Use non-arrow functions for methods that will be using object.method() syntax. (Those are functions that will receive meaningful this value from their caller.)
Use arrow function for almost everything else.

Another tip, in strict mode, this still refers to Window instead of undefined.
(() => {
"use strict";
console.log(this); // window
})();
(function () {
"use strict";
console.log(this); // undefined
})();

This inside arrow function doesn't reflect context of the object. Instead it gives the context where the object method is called.
Check this, This gives some insight about when to use arrow and when not.
https://dmitripavlutin.com/when-not-to-use-arrow-functions-in-javascript/

Related

What is the difference in this both implementation of 'bind'? [duplicate]

In ES6, both of these are legal:
var chopper = {
owner: 'Zed',
getOwner: function() { return this.owner; }
};
and, as shorthand:
var chopper = {
owner: 'Zed',
getOwner() { return this.owner; }
}
Is it possible to use the new arrow functions as well? In trying something like
var chopper = {
owner: 'John',
getOwner: () => { return this.owner; }
};
or
var chopper = {
owner: 'John',
getOwner: () => (this.owner)
};
I get an error message suggesting that the method does not have access to this. Is this just a syntax issue, or can you not use fat-arrow methods inside of ES6 objects?
Arrow functions are not designed to be used in every situation merely as a shorter version of old-fashioned functions. They are not intended to replace function syntax using the function keyword. The most common use case for arrow functions is as short "lambdas" which do not redefine this, often used when passing a function as a callback to some function.
Arrow functions cannot be used to write object methods because, as you have found, since arrow functions close over the this of the lexically enclosing context, the this within the arrow is the one that was current where you defined the object. Which is to say:
// Whatever `this` is here...
var chopper = {
owner: 'Zed',
getOwner: () => {
return this.owner; // ...is what `this` is here.
}
};
In your case, wanting to write a method on an object, you should simply use traditional function syntax, or the method syntax introduced in ES6:
var chopper = {
owner: 'Zed',
getOwner: function() {
return this.owner;
}
};
// or
var chopper = {
owner: 'Zed',
getOwner() {
return this.owner;
}
};
(There are small differences between them, but they're only important if you use super in getOwner, which you aren't, or if you copy getOwner to another object.)
There was some debate on the es6 mailing list about a twist on arrow functions which have similar syntax but with their own this. However, this proposal was poorly received because that is mere syntax sugar, allowing people to save typing a few characters, and provides no new functionality over existing function syntax. See the topic unbound arrow functions.
In this line getOwner: () => this.owner should be:
var chopper = {
owner: 'John',
getOwner: () => this.owner
}; //here `this` refers to `window` object.
console.log(chopper.getOwner());
You would have to declare this into a function:
var chopper = {
owner: 'John',
getOwner() { return this.owner }
};
console.log(chopper.getOwner());
Or:
var chopperFn = function(){
this.setOwner = (name) => this.owner = name;
Object.assign(this,{
owner: 'Jhon',
getOwner: () => this.owner,
})
}
var chopper = new chopperFn();
console.log(chopper.getOwner());
chopper.setOwner('Spiderman');
console.log(chopper.getOwner());
If you have to use arrow function, you can change this to chopper,
var chopper = {
owner: "John",
getOwner: () => chopper.owner
};
Although this is not best practice, when you change the object name, you have to change this arrow function.
A quick tip that I follow to use arrow functions.
Use non-arrow functions for methods that will be using object.method() syntax. (Those are functions that will receive meaningful this value from their caller.)
Use arrow function for almost everything else.
Another tip, in strict mode, this still refers to Window instead of undefined.
(() => {
"use strict";
console.log(this); // window
})();
(function () {
"use strict";
console.log(this); // undefined
})();
This inside arrow function doesn't reflect context of the object. Instead it gives the context where the object method is called.
Check this, This gives some insight about when to use arrow and when not.
https://dmitripavlutin.com/when-not-to-use-arrow-functions-in-javascript/

my arrow functin is not working truly , Why? [duplicate]

In ES6, both of these are legal:
var chopper = {
owner: 'Zed',
getOwner: function() { return this.owner; }
};
and, as shorthand:
var chopper = {
owner: 'Zed',
getOwner() { return this.owner; }
}
Is it possible to use the new arrow functions as well? In trying something like
var chopper = {
owner: 'John',
getOwner: () => { return this.owner; }
};
or
var chopper = {
owner: 'John',
getOwner: () => (this.owner)
};
I get an error message suggesting that the method does not have access to this. Is this just a syntax issue, or can you not use fat-arrow methods inside of ES6 objects?
Arrow functions are not designed to be used in every situation merely as a shorter version of old-fashioned functions. They are not intended to replace function syntax using the function keyword. The most common use case for arrow functions is as short "lambdas" which do not redefine this, often used when passing a function as a callback to some function.
Arrow functions cannot be used to write object methods because, as you have found, since arrow functions close over the this of the lexically enclosing context, the this within the arrow is the one that was current where you defined the object. Which is to say:
// Whatever `this` is here...
var chopper = {
owner: 'Zed',
getOwner: () => {
return this.owner; // ...is what `this` is here.
}
};
In your case, wanting to write a method on an object, you should simply use traditional function syntax, or the method syntax introduced in ES6:
var chopper = {
owner: 'Zed',
getOwner: function() {
return this.owner;
}
};
// or
var chopper = {
owner: 'Zed',
getOwner() {
return this.owner;
}
};
(There are small differences between them, but they're only important if you use super in getOwner, which you aren't, or if you copy getOwner to another object.)
There was some debate on the es6 mailing list about a twist on arrow functions which have similar syntax but with their own this. However, this proposal was poorly received because that is mere syntax sugar, allowing people to save typing a few characters, and provides no new functionality over existing function syntax. See the topic unbound arrow functions.
In this line getOwner: () => this.owner should be:
var chopper = {
owner: 'John',
getOwner: () => this.owner
}; //here `this` refers to `window` object.
console.log(chopper.getOwner());
You would have to declare this into a function:
var chopper = {
owner: 'John',
getOwner() { return this.owner }
};
console.log(chopper.getOwner());
Or:
var chopperFn = function(){
this.setOwner = (name) => this.owner = name;
Object.assign(this,{
owner: 'Jhon',
getOwner: () => this.owner,
})
}
var chopper = new chopperFn();
console.log(chopper.getOwner());
chopper.setOwner('Spiderman');
console.log(chopper.getOwner());
If you have to use arrow function, you can change this to chopper,
var chopper = {
owner: "John",
getOwner: () => chopper.owner
};
Although this is not best practice, when you change the object name, you have to change this arrow function.
A quick tip that I follow to use arrow functions.
Use non-arrow functions for methods that will be using object.method() syntax. (Those are functions that will receive meaningful this value from their caller.)
Use arrow function for almost everything else.
Another tip, in strict mode, this still refers to Window instead of undefined.
(() => {
"use strict";
console.log(this); // window
})();
(function () {
"use strict";
console.log(this); // undefined
})();
This inside arrow function doesn't reflect context of the object. Instead it gives the context where the object method is called.
Check this, This gives some insight about when to use arrow and when not.
https://dmitripavlutin.com/when-not-to-use-arrow-functions-in-javascript/

"this" keyword in an arrow function [duplicate]

In ES6, both of these are legal:
var chopper = {
owner: 'Zed',
getOwner: function() { return this.owner; }
};
and, as shorthand:
var chopper = {
owner: 'Zed',
getOwner() { return this.owner; }
}
Is it possible to use the new arrow functions as well? In trying something like
var chopper = {
owner: 'John',
getOwner: () => { return this.owner; }
};
or
var chopper = {
owner: 'John',
getOwner: () => (this.owner)
};
I get an error message suggesting that the method does not have access to this. Is this just a syntax issue, or can you not use fat-arrow methods inside of ES6 objects?
Arrow functions are not designed to be used in every situation merely as a shorter version of old-fashioned functions. They are not intended to replace function syntax using the function keyword. The most common use case for arrow functions is as short "lambdas" which do not redefine this, often used when passing a function as a callback to some function.
Arrow functions cannot be used to write object methods because, as you have found, since arrow functions close over the this of the lexically enclosing context, the this within the arrow is the one that was current where you defined the object. Which is to say:
// Whatever `this` is here...
var chopper = {
owner: 'Zed',
getOwner: () => {
return this.owner; // ...is what `this` is here.
}
};
In your case, wanting to write a method on an object, you should simply use traditional function syntax, or the method syntax introduced in ES6:
var chopper = {
owner: 'Zed',
getOwner: function() {
return this.owner;
}
};
// or
var chopper = {
owner: 'Zed',
getOwner() {
return this.owner;
}
};
(There are small differences between them, but they're only important if you use super in getOwner, which you aren't, or if you copy getOwner to another object.)
There was some debate on the es6 mailing list about a twist on arrow functions which have similar syntax but with their own this. However, this proposal was poorly received because that is mere syntax sugar, allowing people to save typing a few characters, and provides no new functionality over existing function syntax. See the topic unbound arrow functions.
In this line getOwner: () => this.owner should be:
var chopper = {
owner: 'John',
getOwner: () => this.owner
}; //here `this` refers to `window` object.
console.log(chopper.getOwner());
You would have to declare this into a function:
var chopper = {
owner: 'John',
getOwner() { return this.owner }
};
console.log(chopper.getOwner());
Or:
var chopperFn = function(){
this.setOwner = (name) => this.owner = name;
Object.assign(this,{
owner: 'Jhon',
getOwner: () => this.owner,
})
}
var chopper = new chopperFn();
console.log(chopper.getOwner());
chopper.setOwner('Spiderman');
console.log(chopper.getOwner());
If you have to use arrow function, you can change this to chopper,
var chopper = {
owner: "John",
getOwner: () => chopper.owner
};
Although this is not best practice, when you change the object name, you have to change this arrow function.
A quick tip that I follow to use arrow functions.
Use non-arrow functions for methods that will be using object.method() syntax. (Those are functions that will receive meaningful this value from their caller.)
Use arrow function for almost everything else.
Another tip, in strict mode, this still refers to Window instead of undefined.
(() => {
"use strict";
console.log(this); // window
})();
(function () {
"use strict";
console.log(this); // undefined
})();
This inside arrow function doesn't reflect context of the object. Instead it gives the context where the object method is called.
Check this, This gives some insight about when to use arrow and when not.
https://dmitripavlutin.com/when-not-to-use-arrow-functions-in-javascript/

Question about keyword THIS in javascript [duplicate]

In ES6, both of these are legal:
var chopper = {
owner: 'Zed',
getOwner: function() { return this.owner; }
};
and, as shorthand:
var chopper = {
owner: 'Zed',
getOwner() { return this.owner; }
}
Is it possible to use the new arrow functions as well? In trying something like
var chopper = {
owner: 'John',
getOwner: () => { return this.owner; }
};
or
var chopper = {
owner: 'John',
getOwner: () => (this.owner)
};
I get an error message suggesting that the method does not have access to this. Is this just a syntax issue, or can you not use fat-arrow methods inside of ES6 objects?
Arrow functions are not designed to be used in every situation merely as a shorter version of old-fashioned functions. They are not intended to replace function syntax using the function keyword. The most common use case for arrow functions is as short "lambdas" which do not redefine this, often used when passing a function as a callback to some function.
Arrow functions cannot be used to write object methods because, as you have found, since arrow functions close over the this of the lexically enclosing context, the this within the arrow is the one that was current where you defined the object. Which is to say:
// Whatever `this` is here...
var chopper = {
owner: 'Zed',
getOwner: () => {
return this.owner; // ...is what `this` is here.
}
};
In your case, wanting to write a method on an object, you should simply use traditional function syntax, or the method syntax introduced in ES6:
var chopper = {
owner: 'Zed',
getOwner: function() {
return this.owner;
}
};
// or
var chopper = {
owner: 'Zed',
getOwner() {
return this.owner;
}
};
(There are small differences between them, but they're only important if you use super in getOwner, which you aren't, or if you copy getOwner to another object.)
There was some debate on the es6 mailing list about a twist on arrow functions which have similar syntax but with their own this. However, this proposal was poorly received because that is mere syntax sugar, allowing people to save typing a few characters, and provides no new functionality over existing function syntax. See the topic unbound arrow functions.
In this line getOwner: () => this.owner should be:
var chopper = {
owner: 'John',
getOwner: () => this.owner
}; //here `this` refers to `window` object.
console.log(chopper.getOwner());
You would have to declare this into a function:
var chopper = {
owner: 'John',
getOwner() { return this.owner }
};
console.log(chopper.getOwner());
Or:
var chopperFn = function(){
this.setOwner = (name) => this.owner = name;
Object.assign(this,{
owner: 'Jhon',
getOwner: () => this.owner,
})
}
var chopper = new chopperFn();
console.log(chopper.getOwner());
chopper.setOwner('Spiderman');
console.log(chopper.getOwner());
If you have to use arrow function, you can change this to chopper,
var chopper = {
owner: "John",
getOwner: () => chopper.owner
};
Although this is not best practice, when you change the object name, you have to change this arrow function.
A quick tip that I follow to use arrow functions.
Use non-arrow functions for methods that will be using object.method() syntax. (Those are functions that will receive meaningful this value from their caller.)
Use arrow function for almost everything else.
Another tip, in strict mode, this still refers to Window instead of undefined.
(() => {
"use strict";
console.log(this); // window
})();
(function () {
"use strict";
console.log(this); // undefined
})();
This inside arrow function doesn't reflect context of the object. Instead it gives the context where the object method is called.
Check this, This gives some insight about when to use arrow and when not.
https://dmitripavlutin.com/when-not-to-use-arrow-functions-in-javascript/

Es6 arrow function 'this' in an object [duplicate]

In ES6, both of these are legal:
var chopper = {
owner: 'Zed',
getOwner: function() { return this.owner; }
};
and, as shorthand:
var chopper = {
owner: 'Zed',
getOwner() { return this.owner; }
}
Is it possible to use the new arrow functions as well? In trying something like
var chopper = {
owner: 'John',
getOwner: () => { return this.owner; }
};
or
var chopper = {
owner: 'John',
getOwner: () => (this.owner)
};
I get an error message suggesting that the method does not have access to this. Is this just a syntax issue, or can you not use fat-arrow methods inside of ES6 objects?
Arrow functions are not designed to be used in every situation merely as a shorter version of old-fashioned functions. They are not intended to replace function syntax using the function keyword. The most common use case for arrow functions is as short "lambdas" which do not redefine this, often used when passing a function as a callback to some function.
Arrow functions cannot be used to write object methods because, as you have found, since arrow functions close over the this of the lexically enclosing context, the this within the arrow is the one that was current where you defined the object. Which is to say:
// Whatever `this` is here...
var chopper = {
owner: 'Zed',
getOwner: () => {
return this.owner; // ...is what `this` is here.
}
};
In your case, wanting to write a method on an object, you should simply use traditional function syntax, or the method syntax introduced in ES6:
var chopper = {
owner: 'Zed',
getOwner: function() {
return this.owner;
}
};
// or
var chopper = {
owner: 'Zed',
getOwner() {
return this.owner;
}
};
(There are small differences between them, but they're only important if you use super in getOwner, which you aren't, or if you copy getOwner to another object.)
There was some debate on the es6 mailing list about a twist on arrow functions which have similar syntax but with their own this. However, this proposal was poorly received because that is mere syntax sugar, allowing people to save typing a few characters, and provides no new functionality over existing function syntax. See the topic unbound arrow functions.
In this line getOwner: () => this.owner should be:
var chopper = {
owner: 'John',
getOwner: () => this.owner
}; //here `this` refers to `window` object.
console.log(chopper.getOwner());
You would have to declare this into a function:
var chopper = {
owner: 'John',
getOwner() { return this.owner }
};
console.log(chopper.getOwner());
Or:
var chopperFn = function(){
this.setOwner = (name) => this.owner = name;
Object.assign(this,{
owner: 'Jhon',
getOwner: () => this.owner,
})
}
var chopper = new chopperFn();
console.log(chopper.getOwner());
chopper.setOwner('Spiderman');
console.log(chopper.getOwner());
If you have to use arrow function, you can change this to chopper,
var chopper = {
owner: "John",
getOwner: () => chopper.owner
};
Although this is not best practice, when you change the object name, you have to change this arrow function.
A quick tip that I follow to use arrow functions.
Use non-arrow functions for methods that will be using object.method() syntax. (Those are functions that will receive meaningful this value from their caller.)
Use arrow function for almost everything else.
Another tip, in strict mode, this still refers to Window instead of undefined.
(() => {
"use strict";
console.log(this); // window
})();
(function () {
"use strict";
console.log(this); // undefined
})();
This inside arrow function doesn't reflect context of the object. Instead it gives the context where the object method is called.
Check this, This gives some insight about when to use arrow and when not.
https://dmitripavlutin.com/when-not-to-use-arrow-functions-in-javascript/

Categories