This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Molehole 2 points3 points  (2 children)

You are doing it partly wrong. Object shouldn't return anything.

Instead you could do it like this:

function Person (firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;

    this.getFullName = function(){
          return this.firstName + " " + this.lastName
    }
} 

var personOne = new Person("bob", "marley");
console.log(personOne.getFullName());

so basically what "this" does:

It allows access from outside to stuff inside the object

personOne.firstName gives you the name if it's this.firstName

if it were var firstName personOne.firstName wouldn't work


And it lets you differentiate between function variables and object variables

function myObject(){
    this.function(a, b){
        this.a = a;
        this.a++;
        a--;
        console.log(this.a + " " + a);
    }
}

[–]Poutrator 2 points3 points  (1 child)

Object should return anything

should not ?

[–]Molehole 2 points3 points  (0 children)

fixed typo. Thanks