2

I am trying to understand why the function Fruit works when making an object:

function Fruit(name, color, shape){
    this.name = name;
    this.color = color;
    this.shape = shape;
}

var apples = new Fruit('apple', 'red', 'round');

Why is it not the following:

function Fruit(name, color, shape){
   name = this.name;
   color = this.color;
   shape = this.shape;
}

if, for example, the name after the equals sign is what is pointing to 'apple' and this points to the parameters in the var apples wouldn't it make more sense to put this after?

Sorry in advance if I didn't phrase the question properly.


To clarify why I don't understand it let's change the names so that they aren't the same:

 function Fruit(name, color, shape){
     this.thename = name;
     this.thecolor = color;
     this.theshape = shape;
 }

var apples = new Fruit('apple', 'red', 'round');

And that would still work because the object apples would be {thename: 'apple', thecolor: 'red', theshape: 'round'}

so then isn't it thename = 'apple' if you had thename = this.name in the function?

3
  • 3
    From MDN: "The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand" Commented Oct 24, 2018 at 4:33
  • 3
    softwareengineering.stackexchange.com/questions/98406/… Commented Oct 24, 2018 at 4:34
  • @user64350 I have edited my answer to match your current edits, take a look at it Commented Oct 24, 2018 at 4:53

2 Answers 2

2

To clarify what you are suggesting (edited to match your edits), if we had a function like:

function Fruit(name, color, shape){
    thename = this.name;
    thecolor = this.color;
    theshape = this.shape;
}

then calling

var apples = new Fruit('apple', 'red', 'round');

would mean that:

thename = this.name
thecolor = this.color
theshape = this.shape

Now you are trying to store attributes which do not exist into variables which will not be accessed after the function is called and will probably be garbage collected at the end. In this case the result will have no attributes and will save none of the data that was passed to it.

Your misunderstanding is that the name parameter of the function is accessed using this.name instead of name, here is a clarification:

  • parameters of functions are accessed using whatever name was assigned to them during the functions initial definition
  • attributes are accessed by calling this.attribute

This distinction is made so that it is clear if you are using an attribute or if you are using a parameter.

Sign up to request clarification or add additional context in comments.

5 Comments

I don't this I can format my comment so I'll add it to the question! Thanks for elaborating.
@user64350 You seem to have a slight misunderstanding of what function parameters are and what object attributes are, if you define a parameter of a function using name whenever you want access that parameter you use name, the moment you use this.name you are trying to access an attribute called name
Thanks for elaborating on both misunderstandings. I think it helped a lot!
@user64350 your welcome, if you found the answer helpful can you upvote/accept it ?
I'm one point away from being able to upvote on StackOverflow! Still very new to all of this :)
0

The reason it's only the first way is because the assignment operator = assigns the right hand side to the left hand side, rather than a reference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.