Why I Care about the Semantic Web
dev.to3 pointsby kenbellows1 comments
> true == 1
-> true
> false == 0
-> true
Do you get something different? Or is the game claiming something different? if (Number(myNum) === 2) {}
For me this also extends to using `Boolean(val)` instead of `!!val`, etc.; though I understand the appeal of those nifty one-liners, I think they cause confusion in many places and don't communicate intent nearly as well. 1. create a new empty object; call it `newObj`
2. call the constructor using `newObj` as its `this` context, so that `this.a = 1` is effectively `newObj.a = 1`
3. if the constructor has a `prototype` property defined, invoke `newObj.__proto__ = myconstructor.prototype` to establish the prototype chain on the new object
The critical distinction here is between `constructor.prototype` and `object.__proto__`. For exactly this reason, it bothers me a bit that the article uses `Prototype`, with a capital "P", to mean "the thing that `object.__proto__` points to". This is completely different from the `prototype` (small "p") property of a constructor, which is essentially just a holding place for the `__proto__` property of any objects created by calling this constructor with the `new` keyword. let vertebrate = {hasSpine: true},
mammal = {hasHair: true},
dog = {sound: 'bark'};
sparky = {name: 'Sparky'},
jumbo = {name: 'Jumbo'};
mammal.__proto__ = vertebrate;
dog.__proto__ = mammal;
sparky.__proto__ = dog;
jumbo.__proto__ = dog;
console.log(sparky.hasSpine); // true
console.log(jumbo.hasHair); // true
console.log(sparky.sound); // 'bark'
console.log(jumbo.name); // 'Jumbo'
When you access `sparky.hasSpine`, the JS engine first checks whether `sparky` has an "own property" called `hasSpine`. It doesn't, so it checks `sparky.__proto__.hasSpine`, which is the same as `dog.hasSpine`. No luck, so it checks `sparky.__proto__.__proto__.hasSpine` (i.e. `mammal.hasSpine`), and finally `sparky.__proto__.__proto__.__proto__.hasSpine` (i.e. `vertebrate.hasSpine`), which resolves to `true`.