0

This has been driving me nuts! Its my first time using setters and gettings and im stumped.

var Person = function () {
  var address = setPersonAddress();

  this.setPersonAddress = function (a.getFullAddress()) {
    address = a.getFullAddress();
  };
};

var Address = function () {
  var address = "xyz";
  var city = "xyz";
  var state = "NY";
  var zip = "56783";


  this.fullAddress = function () {
    var name = address + ", " + city + ", " + state + ", " + zip;
    return name;
  };

  this.getFullAddress = function () {
    return fullAddress();
  };
};

var a = new Address();

I am trying to assign the fullAddress from Address object to the Person object address. Am i referencing getFullAddress correctly in setPersonAddress?

3
  • 1
    this.setPersonAddress = function (a.getFullAddress()) { < this makes no sense, you probably want this.setPersonAddress = function (a) { (var address = setPersonAddress(); this too, makes no sense ;)) Commented Apr 26, 2013 at 16:30
  • @Yoshi yes I agree that gives a Unexpected token error. Commented Apr 26, 2013 at 16:33
  • yeah I had a feeling that didnt make sense. Yoshi's suggestion is incorrect also? Commented Apr 26, 2013 at 16:35

1 Answer 1

2

There are many issues with your code and you should lint it to view your syntax errors. Also, you probably want to read more about Object Oriented JavaScript.

However, here's what you are trying to achieve (I think):

DEMO: http://jsfiddle.net/FR8B8/

function Person(address) {
    this.setAddress(address);
}

Person.prototype = {
    setAddress: function (address) {
        this.address = address;
    },
    getAddress: function () {
        return this.address;
    }
};

function Address(fullAddress) {
    this.setFullAddress(fullAddress);
}

Address.prototype = {
    setFullAddress: function (fullAddress) {
        //naive parsing
        var addressParts = fullAddress.match(/(.+),(.+),(.+),(.+)/);

        this.fullAddress = fullAddress;
        this.address = addressParts[1];
        this.city = addressParts[2];
        this.state = addressParts[3];
        this.zip = addressParts[4];
    },

    getFullAddress: function () {
        return this.fullAddress;
    }
};

var a = new Address('15 test,Test City,Test State,J9A333'),
    p = new Person(a);

console.log(p.getAddress());

I am trying to assign the fullAddress from Address object to the Person object address. Am i referencing getFullAddress correctly in setPersonAddress?

That could be done easily, however since you already have an Address value object it's probably better that the Person object encapsulates an instance of an Address object instead of a string.

From the example below, you can get the full address of a person by doing p.getAddress().getFullAddress(). Note that could create a getFullAddress function member on the Person object that encapsulate this logic to simplify things.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.