I read about various ways to do OOP in JS.
Dave Crockford has an interesting approach in which he does not seem to use delegation at all. Instead, it seems to me that it uses purely the concatenation of objects as its mechanism of inheritance, but it's hard for me to say what is happening, and I hope someone can help.
Here is an example that Crockford gives in one of his conversations.
function constructor(spec) { let {member} = spec, {other} = other_constructor(spec), method = function () {
And here is an example from gist
function dog(spec) { var { name, breed } = spec, { say } = talker({ name }), bark = function () { if ( breed === 'chiuaua' ) { say( 'Yiff!' ); } else if ( breed === 'labrador' ) { say('Rwoooooffff!'); } }; return Object.freeze({ bark, breed }); } function talker(spec) { var { name } = spec; var say = function(sound) { console.log(name, "said:", sound) } return Object.freeze({ say }); } var buttercup = dog({ name: 'Buttercup', breed: 'chiuaua' });
I got confused in a few things.
I have never seen object literals before.
- It does not specify key-value pairs and instead just commas separates the strings.
- he uses them on the left side of the assignment
Also, what are the benefits of freezing the returned object?
Lukep source share