Question on this keyword embeded in method

Just a quick question. Why is it necessary to embed this keyword in a function as the value of a property as follow

let duck = {
name: "Aflac",
numLegs: 2,
sayName: function() {return "The name of this duck is " + duck.name + ".";}
};
duck.sayName();

Instead of just defining them as the value of the property like this?

name: "Aflac",
numLegs: 2,
sayName: "The name of this duck is " + duck.name + ".";
};
duck.sayName();
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36 Edg/93.0.961.52

Challenge: Create a Method on an Object

Link to the challenge:

Okay, first, your code doesn’t work. You can’t access your own object from within while defining It, you have to keep in mind that when you call something, that something is read and as in ‘sayName: "The name of this duck is " + duck.name + "’, how to read a property of an object that wasn’t even defined yet?

When you define a function, sayName don’t try to call “duck.name” to concatenate the values, sayName is just defined as function with instructions that will be read only when you call It.

I’ll try to exemplify for you.
As property:
let duck = { // Detect the keyword ‘let’ and start to define the variable.
name: “Aflac”, //Property name assigned to value ‘‘Aflac’’
numLegs: 2, //Property numLegs assigned to value 2
sayName: "The name of this duck is " + duck.name + “.” //Error. Trying to read undefined ‘duck.name’
};

As function:
let duck = { // Detect the keyword ‘let’ and start to define the variable.
name: “Aflac”, //Property name assigned to value ‘‘Aflac’’
numLegs: 2, //Property numLegs assigned to value 2
sayName: function() {return "The name of this duck is " + duck.name + “.”;} //Property sayName assigned to function() {set of instructions}
};//Finished defining variable.

Also, methods are very important tools in javascript. You can define an unlimited number of instructions inside a function. This is also just an introduction, you’ll see a better way to use It in the future.

The duck name one works because there is a variable called duck that you’ve defined.

What if you have many duck objects? What if you create the object using some other function, and it doesn’t have a name?

let duck = {
  name: "Aflac",
  numLegs: 2,
  sayName: function() {
    return "The name of this duck is " + duck.name + ".";
  },
};

// I'll create two new objects based
// on duck:
let duck1 = Object.create(duck)
let duck2 = Object.create(duck)
// Then change the name:
duck2.name = "Flappy";

If I run duck2.sayName(), what comes back is:

"The name of this duck is Aflac."

It does work, it works fine. There is a variable called duck that’s guaranteed to exist when the function sayName (which refers to it) is called.

Well, I think you misunderstood his question, he wants to know why he must declare a function. The first code, he did just copy from the exercise and the second one, which is the one that doesn’t work is his code.

1 Like

I understand what they were asking. It’s this specifically I was replying to, sorry I should have been more clear:

It’s just an object that’s been assigned to a variable. If you define a function that refers to that variable within the object, that variable is guaranteed to exist at runtime, so it can’t not work. The code is not executed when you define it. If you couldn’t do this, it wouldn’t be possible to define recursive functions in JavaScript.

1 Like

If you can, I’m not sure how to do it.


Also, functions and objects are different.
It doesn’t matter what you put inside them. If you don’t call the function, everything will run normally.


Also, this shows that you can’t just put anything inside objects.

No, I never said you could. And what you wrote isn’t the same thing. Look

Won’t work, errors at runtime:

testObj = {
  name: "hi",
  getName: testObj.name,
}

Works because testObj exists and can be evaluated when function is called:

testObj = {
  name: "hi",
  getName() {
    return testObj.name;
  }
}

The latter is what the “duck” code is doing

1 Like


As I said, you’re misunderstanding. Look at his code again, he is not defining a method. He wants to know why he can’t call a property.

1 Like

Ah apologies, I am misreading.

Yes, you can’t call a property, same as you can’t call a variable, it doesn’t make sense

1 Like

No problem, everything you said is right. But we were not talking about the same thing.

Yes, when I read it I think I just automatically assumed the brackets were there for the second one

1 Like

Thank you so much everyone. I love JS even more now thanks to FCC and helpful angels like y’all :mage: :mage:

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.