« first day  last day (14 days later) » 

12:00 AM
this is a special keyword; it's not a variable.
this takes on the value of whatever comes before the dot when you invoke object.method().
pierre.talk is a bit misleading, you could say. It's just a pointer to a function, a piece of code. The function exists on its own. It just happens to have been given the name pierre.talk.
Ok I understood that too.
Are you comfortable with mireille.talk = pierre.talk then?
No it is still better to use a seperate talk, to make it more neutral. the pierre is confusing.
Fair enough. Suppose we started with a clean slate.
12:06 AM
var frenchGreeting = function() { console.log("Bonjour, je m'appelle " + this.name); };
var pierre = { name: "Pierre", talk: frenchGreeting };
var mireille = { name: "Mireille", talk: frenchGreeting };
Are you more comfortable with that?
Much better :D
OK. We can proceed with that for the purposes of this discussion.
Any other questions so far?
No only a side question of would talk : frenchGreeting() have worked if you had

function frenchGreeting() ?
Try it. =)
Ok one sec.
var pierre = { name: "Pierre" , talk: frenchGreeting() };
var mireille = { name: "Mireille", talk: frenchGreeting()};
function frenchGreeting (){
console.log("Bonjour, je m'appelle " + this.name);
12:13 AM
Wait, that's not quite the same thing as var pierre = { name: "Pierre", talk: frenchGreeting };.
people[2].talk() // expecting Mireille to greet me
frenchGreeting refers to the function. frenchGreeting() calls the code.
Take a minute to try again.
Ok now code worked as expected
Usually when I call a function i add the () so that is why I used the () in defining talk
12:17 AM
Good. For this discussion, I chose the var frenchGreeting = function() { … } syntax over the function frenchGreeting() { … } syntax to emphasize how you can freely assign chunks of code to variables in JavaScript.
Any other questions so far?
No continue.
In the last frenchGreeting example, we're still a bit repetitive.
Especially if we start adding other features, like a frenchGoodbye.
It would be handy to have a notion of a "prototypical French-speaking person", and instantiate one of those when needed.
… specializing each object to the extent necessary (the name).
12:23 AM
That's where the prototype comes in. It lets you define a prototypical Francophone, a prototypical Anglophone, etc.
Francophone.prototype.talk = function() { console.log("Bonjour, je m'appellle " + this.name };
You also need a way to construct new Francophone objects.
function Francophone(name) { this.name = name; }
Now you can define many Francophones!
var pierre = new Francophone("Pierre");
var mireille = new Francophone("Mireille");
Yes in your answer you didnt bother with var , but rather you used new Francophone ("Mireille");
I'm only naming the objects pierre and mireille if I care to refer to them that way.
I could just as well leave them "anonymous":
var francophones = [new Francophone("Pierre"), new Francophone("Mireille")];
Aah ok, so you use an array because that would be the only way to identify them using the array index, otherwise you wouldn't know how to get Pierre object?
That is if you didn't name them.
Or is there a concept of object 1, object 2?
There are 75 million to 300 million French speakers in the world. I think stuffing them all into an array anonymously would be good enough. =)
Haha ok
12:32 AM
Similarly, you wouldn't want to give variable names to each Monopoly card, would you?
Uhm not the cards, array indice is enough. If you mean the Chest and Chance cards.
Sorry to get sidetracked with Monopoly again. OK with francophones so far?
We've forgotten about john. Do you think you could throw an Anglophone into the mix?
Yes one sec
var anglophones = [new Anglophone("John")];
12:38 AM
OK. Would you feel comfortable putting all three people into a people array?
Let me think
var people = [francophones[0], francophones[1], anglophones[1]];
That was my attempt, returned an error hmm
You only have one Anglophone. anglophones[1] goes past the end of the array.
yup just realised!
should be 0
Shortcut: var people = francophones.concat(anglophones); would also work.
Do you get the expected results for people[0].talk() and people[2].talk()?
I am still getting error with people[2],

var people = [francophones[0], francophones[1], anglophones[0]];
12:46 AM
Take some time to debug that then.
oh i had that line of code before i created the objects!
Let me know when everything works.
function test(){

var francophones = [new Francophone("Pierre"), new Francophone("Mireille")];
var anglophones = [new Anglophone("John")];
var people = [francophones[0], francophones[1], anglophones[0]];

function Francophone(name) { this.name = name; }
function Anglophone(name) { this.name = name; }
No error but nothing happens either.
Is there a console.log() or other statement to print something?
How embarassing!
12:50 AM
That's OK. We all make silly mistakes. Let me know when to proceed.
Yes you can proceed, your shortcurt worked like a charm too!
Actually, I'm not sure if there's anything more to explain!
Well lets talk about Monopoly then.
You have a people array, and each member knows how to talk in his/her respective language.
The implementations use prototypes to avoid code duplication.
That's analogous to picking a card and having the card know what to do.
The only thing that might be tricky is that the card's act() function needs to take a player parameter.
In my code there is global variable called player, this changes whenever i change turns
12:57 AM
But I assume you are comfortable with that. It's just like how any function can take parameters.
I'd pass the player explicitly as a parameter to the act() function nonetheless.
That's better than having act() reach out into the global scope to discover the current player.
Would you be interested in doing an online tutorial of the Monopoly game? That is if you have your own site and free time.
function pickCard(array){
var x = cardCount[array][0];
var title = this[array][x].title;
console.log(x + 1 + ". " + title);
if (cardCount[array][0] === this[array].length){
cardCount[array][0] = 0;
// i still dont know what needs to go here now that the card has been picked?
What do you mean by online tutorial?
By that I mean you develop the game, and post your code in blog posts and let people follow along.
The code I had in my answer was actually a pretty complete solution to the draw-a-card-and-perform-the-action problem. Take some time to read and understand it.
Ok I will.
1:03 AM
and I think that what we covered here should be sufficient to understand all of the code.
function drawCard(player, deck) {
var card = deck.shift();
I just want to talk through this bit of code.
Sure. What puzzles you?
drawCard would be called when the player lands on either the chance or chest positions. that make sense.
what is deck.shift()?
also when i call drawCard I would call it like so : drawCard(player, type)

var type = positions[getCurrentpos()].type;
1:10 AM
At some point, you have to decide which deck (which array) to draw from.
I think that whoever calls drawCard() should have to refer to the relevant deck (array).
What other types of positions are there?
there are 40 positions on the board, but if you mean what types of positions require a card to be drawn then it is only chest and chance positions.
Then do var deck = positions[getCurrentpos()].deck; drawCard(player, deck); instead?
.type you mean?
… where deck is either chanceCards or chestCards.
i changed my array names to "chance" and "chest" so it correlates with my .type property
1:17 AM
Instead of a .type property, you could refer directly to one of the decks.
Or, you could use a layer of indirection…
How so?
var deckForPosition = { 3: chance, 8: chest };
drawCard(player, deckForPosition[getCurrentpos()]);
No I will stick with the .type for now
That's fine. I think we've covered the original topic quite thoroughly, so I'll leave it at that.
Good luck! Any upvotes you could throw my way would be appreciated. =)
Ok I will do.
Thank you very much for your help
Wow I dont even know how to upvote :S
1 hour later…
2:44 AM
room topic changed to JavaScript polymorphism: Tutorial explaining answer codereview.stackexchange.com/a/40077/9357 [dry] [game] [javascript] [polymorphism]

« first day  last day (14 days later) »