-1

I've written this piece of code to try help me understand how objects work in js a bit more.

function person(personName){
  var thiz = this;
  var nameOfMe = (typeof(personName) === 'undefined')? 'default':personName;
  var faveFood = 'stuff';

  thiz.speakName =function(){
    alert('I am '+ thiz.nameOfMe);
  }

  thiz.gotFoodAlert = function(){
    alert('Yummy! I haz ' + thiz.faveFood )
  }
}

var someGuy = new person('joe');
someGuy.faveFood = 'cheesecake';
someGuy.speakName();

var elseGuy = new person();
elseGuy.nameOfMe = 'bob';
elseGuy.speakName();

I'm trying to simulate a classical inheritance model to build a class then instantiate a person. Setting it separately in elseGuy.speakName() alerts 'bob' ok.

What I don't understand is why doesnt the someGuy.speakName() alert 'joe' when I instantiate?

update: on further reflection and taking notes from people commented, I should just give up trying to simulate a classical inheritance model.

3 Answers 3

2

Because nameOfMe is not a property of this in the first example. Try the following instead:

function person(personName) {
  var nameOfMe = (typeof personName === 'undefined')? 'default':personName;
  var faveFood = 'stuff';

  this.speakName = function () {
    alert('I am ' + nameOfMe);
  }

  this.gotFoodAlert = function () {
    alert('Yummy! I haz ' + faveFood )
  }
}

Even better:

function Person(personName) {
  this.nameOfMe = personName ? 'default' : personName;
  this.faveFood = 'stuff';
}

Person.prototype.speakName = function () {
    alert(this.nameOfMe);
};

Person.prototype.gotFoodAlert = function () {
    alert('Yummy! I haz ' + this.faveFood);
};
Sign up to request clarification or add additional context in comments.

2 Comments

No, it works, just one small typo in the better version: this.nameOfMe = personName ? personName : 'default';
Your second version isn't really better. The first has something like private members whereas the second only has public ones.
1

You have to set nameOfMe as a property of thiz:

thiz.nameOfMe = (typeof(personName) === 'undefined')? 'default':personName;

Also, unless you absolutely have to, why don't you use the normal this instead of thiz?

Comments

1
  1. thiz = this aliasing is unnecessary here. You only need it for "private" properties by additional closures.
  2. thiz.nameOfMe is causing an additional closure here, needlessly.
  3. The value of thiz.nameOfMe is not "joe" because the object referred to by thiz does not have a nameOfMe property yet. The nameOfMe variable in constructor code is something else.
  4. You are not using "the classical inheritance model". There are no classes. You are creating a person instance, that is, an object that has person as its constructor, and the object that person.prototype currently refers to next in its prototype chain. Nothing more, nothing less.
  5. It is good code style to have constructor identifiers start with a capital letter: Person.

RTFM.

1 Comment

PE, you are going to have fun here. :-)

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.