[00:00:34] So the three basic operations on object are get, set and delete. Get uses the dot notation or the bracket notation. It turns out they're fairly interchangeable, which is different than most other languages generally brackets and dots have different roles. Here they both do the same thing. So you can either have a constant name or you can have an expression,, which resolves into a string which is that name.
[00:01:01] They both do the same thing, then set is the same thing, except used with an assignment operator. Then delete is a prefix operator, which is rarely used. But if you do use it, you just put it in front of a name of a property and it causes the property to be removed from the object.
[00:01:43] In ES5, we added a meta API which allows you to do the same things. You can say, Object.defineProperties passing an Object.create and Object.prototype and foo. That does exactly the same thing. You might ask well, thank you so much for allowing us to write it this way. What's the value of that?
[00:02:04] The value is that you've got more control now that you can decide what the thing inherits. In particular, you can have it inherit from null now,, so that there's no inheritance happening at all which for some applications turns out to be really beneficial. Also we've got these three attributes, which you now have control over.
[00:02:22] They've always been in the language. But until ES5, you couldn't get at them. So you can now make a property be read only. You can now make it be not enumerable, so that it will not show up in a for in iteration.
>> Douglas Crockford: So most object languages have classes.
>> Douglas Crockford: So in this language, you work with prototypes and it turns out working with prototypes is much easier than working with classes.
[00:03:35] So you make a new object that you like and you can create new instances of that language that inherit from that object using Object.create and then you can customize the new objects, adding new properties or modifying properties as you need to and that's it. That's all you have to do to manage your objects.
[00:03:54] Whereas in a classical system, you first have to classify everything. You have to make a taxonomy. You have to figure out what the relationship is gonna be between all of your classes. And usually at the time you're doing that, you don't not yet have perfect domain knowledge as to how it's going to work out.
[00:04:09] So you make guesses. And invariably, you get stuff wrong which then means you have to work with an inefficient object system or you have to refactor and refactoring is difficult and error prone. All of that is completely avoided in the prototypal system. You just make objects. So having that level of indirection removed just makes everything easier.
[00:04:33] So this is sometimes called delegation where an object can say anything that I don't know how to do, ask that object to do it for me. So objects inherit directly from other objects. So there's no classes getting in the middle. It's also sometimes called differential inheritance where each new object only has to contain the material necessarily to distinguish it from the thing that inherited from.
[00:06:00] So I've got my little bumper count function and then a parse at each word, and.
>> Douglas Crockford: Wait a minute.
>> Douglas Crockford: Okay, so the naive way to have written as would be to simply bump word_count for every word that I get. But that would fail if the word was constructor, because it turns out every object inherits the constructor property.
[00:06:33] And so that's a function. And if you try to add one to a function, then you get weirdness. You don't get one. So it would fail. So you would have to instead look at.
>> Douglas Crockford: What the value was an d look at its type. Make sure it's a number.
[00:06:53] And if it is a number, then I can increment it. If it's not, then I have to assign it. So there's some defensive programming that you have to do which should have been unnecessary, but is necessary, because of unexpected consequences of inheritance.
>> Douglas Crockford: That description expired and that we can now inherit from null that solves a problem for this case.
[00:07:17] Functions inherited from a prototype are included in the for in enumeration, which is usually surprising. Usually when they doing for in, it's because you've got a collection of similar values and you're not expecting any methods to be included in the numeration. And because you don't have control in your code of everybody who might ever add anything to any prototype, it is unfortunately necessary to filter the results in for in to make sure that you're not getting stuff that you don't expect.
[00:07:46] You can be testing for the type of things as we did in the earlier one or looking to see if it's part of hasOwnProperty, which means it's not an inherited property. You really have to do this. Otherwise, there is a possibility that your code's going to explode in the future.
>> Douglas Crockford: One problem with hasOwnProperty is that it's a method and not an operator. So if it turns out that hasOwnProperty is also the name of a data value, then it's not gonna be available as a method. And so you gonna get failure then. So the way to get around that is to find a place to where it's guaranteed to be, which is Object.prototype.hasOwnProperty and then call method passing in the other stuff which is horrible.
[00:08:36] But in the worst case, that's the most reliable way of doing that filtration.
>> Douglas Crockford: Keys must be strings, which I think is one of the errors in the language. It would have been much better if keys could have been any value in particular objects, so you could have associations over objects.
[00:08:55] Unfortunately, Brendan didn't do that. So when you pass it a key, it will convert it to a string automatically coursing it which works, but its lacks some of the power that it could have had otherwise. So there are lot of other types in the language, which all inherits from object.
[00:09:17] Everything is an object. So numbers, Booleans, strings, arrays, dates, regular expressions and functions are all objects in this language.