OLOO questions
- Can you delegate to more than one object?
- So bar cannot have an init function?
He moved the final
init
to Foo
. Or, if the question was about shadowing, then he says it's really not a good idea, it will create a lot of brittle complexity.
There are to steps to "create" your object:
var b1 = Object.create(Bar);
b1.init("b1");
- What about performance? Doesn't we have many instances of an object (
Object.create()
) versus delegating with the prototype chain?We don't have many instances. It's the exact same mechanism as with the
prototype
. There's still just one init
function.The usual pattern is that a central object holds the methods and it's the leaf notes that hold state and delegate to the central object.
- Real world example of OLOO style?
It was still very new when he held the workshop.
- Explain
object.Create()
will do later
- What objections does he get when advocates object delegation vs classes?
People think that they've been taught that classes are the only proper way to design software. That real-world problems map best to classes (wrong).
What he proposes is that there these two patterns, and that classes go against how JS works and delegation is how the language was designed to work.
The diagram of the OLOO pattern.Note that there are no functions, only objects
diagramWe directly create the object and their relationshipsThe polyfill for Object.create()
:
if(!Object.create) {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}