-1

AFAIK "this" is used when there is clash between "property" & "parameter" of function.
Please find my code below.

<script>
    function person(firstName, lastName, age, eyeColor) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.fullName = function() {
            return this.firstname + " " + this.lastname
        }
    }
    var p = new person();
    document.write("fullname is " + p.fullName());
</script>

Here please correct me if I am wrong with following things

this.firstName contains "this" because person contains firstName as parameter and wants to be as property of person.
My doubt is that this.fullName also has "this" in it even thought there is no parameter with fullName.May be I am wrong(Read that "this" actually creates an instance).
If above is true that it actually creates an instance then my problem is it is creating instance in var p = new person() and why would we create instance of fullName property inside of person

ashishmohite
  • 1,120
  • 6
  • 14
  • Possibly related: [Use of 'prototype' vs. 'this' in JavaScript?](http://stackoverflow.com/questions/310870/use-of-prototype-vs-this-in-javascript) If I'm perhaps following your question, it isn't necessary to set `fullName` within `person`. It can be set on `person.prototype` and apply to all instances of `person`. – Jonathan Lonowski Nov 22 '15 at 04:23
  • 1
    Hint: JavaScript is case-sensitive. – Joseph Myers Nov 22 '15 at 04:29
  • just a question, shouldn't the parameters be set when creating an instance of person? like: `var p = new person('foo,'bar');` http://jsfiddle.net/Mi_Creativity/sLoqumz2/ – Mi-Creativity Nov 22 '15 at 04:34

3 Answers3

0

Hope this helps

  1. this.firstname contains "this" because person contains firstname as parameter and wants to be as property of person. - wrong

this is used to assign any value to the property of the object (instance of Person class)

this.firstName = your_var this will assign value of your_var to firstName property of Person's object it doesn't matter if your_var is in parameters or some local var created by you You can assign any value to property of an object

  1. Read that "this" actually creates an instance - wrong

this refers to the created instance and does not create instance

  1. you are not creating instance full name here, what you are doing is creating an instance method for person object which you can later on use to get full name of the person
ashishmohite
  • 1,120
  • 6
  • 14
0

You'll notice if you run this code you get the following error:

Uncaught TypeError: p.fullname is not a function on line 10

There is an error in the capitalization of your method call. Corrected code is below:

document.write("fullname is " + p.fullName());
Tek
  • 153
  • 1
  • 7
0

your properties are like fooBar and you are returning them as foobar, these places:

this.firstName = firstName; // firstName
this.lastName = lastName; // lastName
//but below as firstname, lastname instead of firstName and lastName
return this.firstname + " " + this.lastname

also on here:

    this.fullName = function(){ ... }

but you call the function as p.fullname()


Also I think, since the fullName() returns firstName and lastName, you probebly need to pass these values when creating the instance if you are going to call the fullName() because this function requires them, otherwise you'd get undefined, like below:

var p = new person('Foo', 'Bar');

Now it is working: JS Fiddle

function person(firstName, lastName, age, eyeColor) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.fullName = function() {
    return this.firstName + " " + this.lastName
  }
}
var p = new person('Foo', 'Bar');
// check the console
console.log("fullname is " + p.fullName());
Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47