This question already has answers here:
Methods in ES6 objects: using arrow functions
(6 answers)
Closed 2 years ago.
I have an object like this.
var v= {
a: 1,
delete: () => delete this.a,
}
I am not able to delete a after executing v.delete
You need convert arrow to function to use this keyword.
Also you did not call delete method to delete property.
var v= {
a: 1,
delete: function () {
delete this.a
}
}
v.delete()
console.log(v)
var v= {
a: 1,
delete: function(){delete this.a}
}
console.log("before=" + v.a)
v.delete()
console.log("after="v.a)
Arrow function doesn't have access to this context of the current object.
Related
This question already has answers here:
How does the "this" keyword in Javascript act within an object literal? [duplicate]
(4 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 3 months ago.
My way of binding not working. please correct me.
const ob = {
name:'arif',
getName:() => {
console.log(this)
return this.name;
}
}
const x = ob.getName.bind(ob);
console.log(x()); //return the global name!!
Note:- Here even without binding your function will be taking this as ob only if you call it directly on object.
Arrow function do not have this, normal function do have
const ob = {
name:'arif',
getName: function(){
console.log(this)
return this.name;
}
}
const x = ob.getName.bind(ob);
console.log(x());
The Arrow function doesn't have an argument property of its own, the bind will fail with Arrow function.
instead, you can use normal function here:
const ob = {
name:'arif',
getName: function() {
console.log(this)
return this.name;
}
}
const x = ob.getName.bind(ob);
console.log(x()); //returns the object name !!
This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 1 year ago.
let obj = {
one: function(){
this.two();
},
two: function(){
console.log("two");
}
}
let oneFunc = obj.one;
obj.one(); // two
oneFunc(); // TypeError: this.two is not a function
I have been programming in JavaScript for quite some time. I thought I had a really solid grasp on the this keyword. However, I ran in to this today and it blew my mind. I can't figure out why this is happening. When I call oneFunc(), this refers to the global object. Why does this happen?
this refere to the object, and what you are doing here is creating a function equal to the methode of the object and so you lose the reference to the object.
You can use bind
let obj = {
one: function(){
this.two();
},
two: function(){
console.log("two");
}
}
let oneFunc = obj.one;
oneFunc = oneFunc.bind(obj);
obj.one(); // two
oneFunc(); // TypeError: this.two is not a function
This question already has answers here:
Is it possible to initialize an object as a function with properties?
(2 answers)
Closed 1 year ago.
I know that I can create a function:
const fn = () => {}
And I know that I can attach a property to it:
fn.a = 'a'
Can I use some kind of object literal syntax to do what I did above in one statement?
I'm thinking something along the lines of:
const fn = {
(): {},
a: 'a'
}
You could do it in one statement like this.
const fn = Object.assign(() => {}, { a: 'a' });
How about we make a little utility function to accomplish that?
function createFunc(func, props) {
Object.keys(props).forEach((key) => (func[key] = props[key]));
return func;
}
const f = createFunc(() => console.log("hello"), { a: "A", b: "B" });
f();
console.log(f.a);
console.log(f.b);
This question already has answers here:
Methods in ES6 objects: using arrow functions
(6 answers)
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 1 year ago.
let greetings = {
fullName : "elham zeinodini",
getFullName : () => {
return this.fullName;
},
Greet : message => console.log(`${message} ${this.getFullName()} !!`)
}
console.log(greetings.fullName);
greetings.Greet("Hello");
let greetings = {
fullName : "elham zeinodini",
getFullName() {
return this.fullName;
},
Greet(message) {
console.log(`${message} ${this.getFullName()} !!`)
}
}
console.log(greetings.fullName);
greetings.Greet("Hello");
It means, quite literally, that the property getFullName on the context this is not a function. You're trying to invoke something that isn't a function.
Why is that? Because you can't use this like you are to refer to the surrounding object using an arrow function. Depending on your environment, it might refer to the window object (in the browser). And the window object doesn't have a function called getFullName. So that property evaluates to undefined. Which isn't a function, hence the error.
Use a "regular" function declaration instead. getFullName: function() {...}
The issue is with your arrow functions, which don't allow you to capture the this context.
Rewrite as follows:
let greetings = {
fullName : "elham zeinodini",
getFullName() {
return this.fullName;
},
Greet(message){
console.log(`${message} ${this.getFullName()} !!`)
}
}
console.log(greetings.fullName);
greetings.Greet("Hello");
This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 2 years ago.
I have defined a function which returns object which has property and a method. using the returned object I am calling the method inside the object to get the value of the property in the object. but I didn't received what I expected.
function test(){
return {
a:'test property',
fn:()=>{
console.log('property', this.a);
}
}
}
const obj = test();
obj.fn() // what I expect is test property but I received undefined.
You have declared the function incorrectly. Remember that arrow functions do not have their own this like a regular function does.
You should declare it like this:
function test() {
return {
a: 'test property',
fn() {
console.log('property', this.a);
},
};
}
const obj = test();
obj.fn();
When you use a regular function this is scoped to how it is called. example
obj.fn()
obj is taken as your this
when you define using arrow function this is assigned on creation which points to the parent's context in your case its window