+ 1

Pls what is (this) key word used for in JavaScript I think it is used for updating the arguments of our function Is it correct

20th Jan 2022, 5:54 PM
Okere Samuel
Okere Samuel - avatar
13 odpowiedzi
+ 3
'this' meaning there to 'current object'. Remaining all are your choice, how to use it, as how you program? Ex: console.log(this) ; //to print current object console.log(this.age) ; //printing age this.age= 30 //assigning or modifying..
20th Jan 2022, 7:16 PM
Jayakrishna 🇮🇳
+ 2
"this" refers to the object's properties. It would be helpful if you gave an example code
20th Jan 2022, 6:07 PM
Lisa
Lisa - avatar
+ 2
Thank u alot alot!!! U are gud and u provide understanding answers
20th Jan 2022, 7:24 PM
Okere Samuel
Okere Samuel - avatar
+ 1
function person (name, age) { this. name="A" this. age =20 this.ChangeName = function (name){ this.name = name // above last 2 lines : this.ChangeName here this refers to person function. (So equal to Person.ChangeName) And this.name , here this refers to ChangeName ( equal to ChangeName.name).. It can be used in any object to refer that object.
20th Jan 2022, 6:26 PM
Jayakrishna 🇮🇳
0
OK like Function person (name, age) this. name this. age this. Change Name = function (name) this. name = name let p = name (David, 21) P. ChangeName ("John")
20th Jan 2022, 6:13 PM
Okere Samuel
Okere Samuel - avatar
0
"this" refers to the current object on which now you are refering.. If you use it in function, then this refers to function. On object, refers to object. On window, outside of any function, refers to window ......... edit: Okere Samuel in your code, there this refering to function person in 2nd and 3rd line and in this.name=name, this refers to ChangeName property
20th Jan 2022, 6:16 PM
Jayakrishna 🇮🇳
0
Buh were is (this) mostly useful and is it only in functions that we can use it?
20th Jan 2022, 6:19 PM
Okere Samuel
Okere Samuel - avatar
0
Ok does it mean that the (this) after using it to refer to an object then we can now assign it to a variable in other to make changes to the name?
20th Jan 2022, 6:45 PM
Okere Samuel
Okere Samuel - avatar
0
function person (name, age) { this.name = name; // this. Change Name = function (name) this.name = name } let p = new person(David, 21) P. ChangeName ("John") From this code, see this.name = name; here 2 variables have same named as name. So to refer Person object name, we use this.name and just name variable refers to passed variable. Otherwise name=name is useless, no effect. To distinguish we use 'this' there. First you set name = "David" by person("David", 20) ; and you are going to set changedName as by ChangesName.name = "John"; Hope it helps.
20th Jan 2022, 6:58 PM
Jayakrishna 🇮🇳
0
Ok does it mean that the (this) after using it to refer to an object then we can now assign it to a variable in other to make changes to the name?
20th Jan 2022, 7:02 PM
Okere Samuel
Okere Samuel - avatar
0
And is this(this) only for updating our function arguments
20th Jan 2022, 7:07 PM
Okere Samuel
Okere Samuel - avatar
- 1
Functions are kind of weird in javascript. They are somewhat behave like an object, that's why you can call "this" inside a function. "this" means referring to the current object. You usually use "this" when you want to define properties, functions/methods to that object. Functions are still functions. The behavior only change when you call "this" ex: /** with this */ function add(x, y) { this.propX = x; this.propY = y; return x +y; } typeof add; // "function" const z = add(1,2); z // "3" const x = new add(1,2); x // "Object { propX: 1, propY: 2}" x.propX // "1" x.propY // "2" --------------------------- /** without this */ function add(x, y) { return x +y;} typeof add; // "function" const z = add(1, 2); z // "3" const x = new add(1, 2); x // "Object { }" x.propX // "undefined" x.propY // "undefined"
22nd Jan 2022, 1:26 PM
Jonathan T Rantael
Jonathan T Rantael - avatar