I’m playing with ex21, the first OOP lesson.
I wanted to see if I could create objects with the properties “out of order”.
IE
- call the object constructor
- set an anonymous function as a property (essentially a method) which references another property, “name”
- declare and assign property, “name”
I wondered if I could break the instantiation by referencing “name” in the anonymous function before “name” is declared. It turns out it doesn’t cause any problems. I guess the references only kick in once the method/anon-function is called.
Here’s my experiment using Zed’s code in both JavaScript and Python
#!/usr/bin/env node
// riffing on Zed's example
// we need a way to build these automatically
const Person = (name, age, eyes) => {
// This makes an obj for the data
let obj = {
//name: name,
age: age,
eyes: eyes,
talk: (words) => {
// coolest part is obj here will keep a reference
// Aus:will this break because the obj isn't instantiated yet?
console.log(`I am ${obj.name} and ${words}.`);
},
name: name,
}
// and return our new person
return obj;
}
let chuck = Person('Chuck', 24, 'purple');
chuck.talk("how about a hug for Chuck?");
Here’s the same idea written in python.
#!/usr/bin/env python3
# riffing on Zed's example... IN PYTHON!
# we need a way to build these automatically
class Object(object):
pass
def Person(name, age, eyes):
# Aus: unfortunately, I had to create the Object class because the built-in
# object class doesn't have a __dict__ attribute.
person = Object()
person.__dict__ = {
'age': age,
'eyes': eyes,
# A:will this break because person isn't instantiated yet?
'talk': lambda w: print("My name is", person.name, "and", w)
}
person.name = name
return person
chuck = Person('Chuck', 24, 'purple')
diane = Person('Diane', 33, 'grey')
chuck.talk("how about a hug for Chuck?")
diane.talk("forget about it.")
I have a feeling that I don’t even need to declare the “name” property in the constructor function. I can probably tack it on after a Person is instantiated so long as I do so before the “talk” method is called. Just guessing tho… Aw, why don’t I just try it out in Python to see if it works? …
#!/usr/bin/env python3
# riffing on Zed's example... IN PYTHON!
# we need a way to build these automatically
class Object(object):
pass
def Person(name, age, eyes):
# A: unfortunately, I had to create the Object class because the built-in
# object class doesn't have a __dict__ attribute.
person = Object()
person.__dict__ = {
'age': age,
'eyes': eyes,
# A:will this break because person isn't instantiated yet?
'talk': lambda w: print("My name is", person.name, "and", w),
'flirt': lambda : print(person.name,":", person.charm)
}
person.name = name
return person
chuck = Person('Chuck', 24, 'purple')
diane = Person('Diane', 33, 'grey')
chuck.talk("how about a hug for Chuck?")
diane.talk("forget about it.")
chuck.charm = "<cheesey smile>"
chuck.flirt()
diane.charm = "<rolls eyes>"
diane.flirt()
And here’s the output.
My name is Chuck and how about a hug for Chuck?
My name is Diane and forget about it.
Chuck : <cheesey smile>
Diane : <rolls eyes>
So my guess was correct. Moving on to ex22!