[personal profile] horrorcheck
I posted my weekly checkin to the TLC mailing list last night, but it was mostly whiny complaining about how I didn't get anything done, so I'm not going to repeat that here.

Instead I just want to talk about something in JavaScript that confuses me. (Maybe just because I'm not used to imperative programming.)

So, I want to represent some comics and comic book characters. So I make some objects.
var tony = { name: "Tony", job: "billionaire playboy philanthropist" }
var pepper = { name: "Pepper", job: "CEO of Stark Industries" }

and I say that those are the characters:
var characters = [ tony, pepper ]

and then I make up some comics:
var ironMan = { title: "Iron Man", characters: characters };
var pepperAdventures = { title: "Adventures of Pepper", characters: [pepper] };

The only problem is, Pepper isn't CEO of Stark Industries in (not real comic) "Adventures of Pepper". It's an Alternate Universe where she is a Professional Adventurer. But that's easy to fix, right?
pepperAdventures.characters[0].job = "Adventurer";

And if I look at pepperAdventures now, I see that Pepper's job is correct!
> pepperAdventures
{ title: 'Adventures of Pepper',
  character: { name: 'Pepper', job: 'Adventurer' } }

But this is where I get confused -- because Pepper is now an adventurer in Iron Man too!!
> ironMan
{ title: 'Iron Man',
  characters: 
   [ { name: 'Tony',
       job: 'billionaire playboy philanthropist' },
     { name: 'Pepper', job: 'Adventurer' } ] }

This is not good!! Stark Industries is sure to go bankrupt without Pepper!

I guess I just find this confusing because I'm not able to alter values like this in Haskell. This is what something similar might look like in Haskell:
data Character = Character { name :: String, job :: String }
data Comic = Comic { title :: String, characters :: [Characters] }

tony = Character "Tony" "billionaire playboy philanthropist"
pepper = Character "Pepper" "CEO of Stark Industries"
characters = [ tony, pepper ]

ironMan = Comic "Iron Man" characters
pepperAdventures = Comic "Adventures of Pepper" [ pepper { job = "Adventurer } ]

This results in the expected:
>pepperAdventures
Comic {
  title = "Adventures of Pepper", 
  characters = [
    Character {name = "Pepper", job = "Adventurer"}]}
>ironMan
Comic {
  title = "Iron Man", 
  characters = [
    Character {name = "Tony", job = "billionaire playboy philanthropist"},
    Character {name = "Pepper", job = "CEO of Stark Industries" }]}

That's because pepper {job = "Adventurer"} creates a new and different Character that's the same as pepper except that the "job" field is "Adventurer". It doesn't change the original pepper at all.

So it's constantly surprising when I run into these side effects in JavaScript and I find them kind of hard to understand. HOWEVER I can see how this could also be super useful and make things easier than what I'm used to!!
From:
Anonymous( )Anonymous This account has disabled anonymous posting.
OpenID( )OpenID You can comment on this post while signed in with an account from many other sites, once you have confirmed your email address. Sign in using OpenID.
User
Account name:
Password:
If you don't have an account you can create one now.
Subject:
HTML doesn't work in the subject.

Message:

 
Notice: This account is set to log the IP addresses of everyone who comments.
Links will be displayed as unclickable URLs to help prevent spam.

Profile

horrorcheck

August 2015

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829
3031     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 26th, 2017 08:54 am
Powered by Dreamwidth Studios