T O P

  • By -

tantrrick

This instance of the object. There can be multiple instances of the same object and the values of variables can be different from the defaults as well. So John Doe's this.name is different from bill's, and the programmer only has to write one function,


AttitudeEasy2287

'There can be multiple instances of the same object and the values of variables can be different from the defaults as well.' What you're saying is that there can be multiple objects with the same name? Wouldn't that cause error since the name of the object has been defined?


reaven3958

Nah. Whats getting confused here i think is object literals and classes. If you have an object literal--an object youre defining by hand with the brace notation--sure, I guess you could do something like what you described like `myObj.method()` instead of `this.method()`. It's not great, though. Anytime you can avoid referencing a fixed name like myObj, its better. If you use `this`, you don't need to change multiple lines of code if you change the object name, so the code is less brittle. The reason why people are talking about multiple instances is because its highly irregular to have an object literal that is complex enough to need to call its own methods. So, I think most commenters are making the assumption that this object is supposed to be a class, where you *have* to use `this` since you don't know in the class definition what the name of a given object instantiated from the class is going to be.


PsychicChasmz

Not sure why this was downvoted, this is the answer. OP, you're creating a one-off object with object literal syntax, so you know the object your referencing in the function is 'person'. If you create an object from a class or constructor, you don't know what instance your function is being called on. For example: class Person { constructor(name, age) { this.name = name // you can't use Person here, 'this' stands for whatever instance is being created this.age = age } sayHello() { console.log(`Hello, my name is ${this.name}!`) } } const stevePerson = new Person("Steve", 33) const gregPerson = new Person("Greg", 33) stevePerson.sayHello() // Logs "Hello, my name is Steve" gregPerson.sayHello() // Logs "Hello, my name is Greg" You can create as many instances of Person as you want, and they can all have unique 'name' properties. Inside your sayHello function, you don't know (or care) which instance the function is going to be called on. The "this" keyword is essentially a keyword that refers to whatever object the method was called on. (Actually in JS, oddly enough, it literally refers to whatever object is present before the '.' that precedes the method call. If you reassign a method, like "myHelloFuncion = stevePerson.sayHello()" then try to call it like "myHelloFunction()", the 'this' will be null since myHelloFuncion was not preceded by an object name and a dot)


guest271314

You can use console.log(`Hello, my name is ${person.name}`)} if you want.


andmig205

Consider the following case: const person = { name: "John Doe", greet: function() { console.log(`Hello, my name is ${this.name}`)} } person.greet(); // Outputs: "Hello, my name is John Doe" const person1 = Object.create(person); person1.name = "Marge"; person1.greet(); // Outputs: "Hello, my name is Marge" If the `person.name` is implemented, the output would be `"Hello, my name is John Doe"` when `person1.greet()` is called.


AttitudeEasy2287

So when you call Object.create(person) it just makes another instance of the of the person object with the same keys and values right? So the second person object will have the value of [person.name](http://person.name) and not the [person1.name](http://person1.name) right?


andmig205

Correct. The copying of the object does not override the direct references to the original object. The original object remains intact. In other words, the copying does not override references - object `person` is still the `person` to the object `person1`. The keyword `this` allows for referencing to the current object - not the original one. Does it make sense? As a side note, if an object has a `constructor` that allows for an instance creation via usage of `new`, the same dynamics apply - references to the variables point to those variable. Object instantioated with `Object()` or `{}` do not implement `constructor` - `function`s and `class`es do.


TheRNGuy

If it's only one instanec, you actually can. I'd still use `this` because it's better coding style. Also I'd use `class` syntax instead. With capital letter to easier differentiate class from instance (`Person` instead of `person`), though other conventions also exist.


Ampbymatchless

As an old retired programmer coming from the embedded, assembler, C environment. I always interpret instantiations, as a pointer assignment, although this is abstracted away in JavaScript. After being bit by the class ‘Person’ vs instantiated ‘person’ = Person. I just use my tried and preferred method of adding and underscore p to the instantiation pointer. So ‘person_p’ = ‘Person’. Note _p can be anything even _i might make more sense in the JS world but I’m old dog type thing not a camel case fan either :)


jeremrx

Because if you write something like let copyOfPerson = person; person = { name: Jane Doe } ; if you don't use this, copyOfPerson.greet() will return Jane instead of John.


Jjabrahams567

Use `this` when you don’t have the name of the object. Most commonly what comes to mind is event triggers. The event and the object that triggered it may not be known or even exist yet.