Last active
August 21, 2020 21:48
-
-
Save wentout/fabb8e35520f84fb091bae38750567e5 to your computer and use it in GitHub Desktop.
representing null as constructor prototype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const MyNull = function () {}; | |
MyNull.prototype = null; | |
const s = new MyNull(); | |
// as this might be only the object, | |
// so null would be converted | |
console.log(s); | |
console.log(s.valueOf()); // {} | |
const s_proto = Object.getPrototypeOf(s); | |
console.log(s === s.valueOf()); // true | |
console.log(s_proto); // [Object: null prototype] {} | |
console.log(s instanceof Object) // true | |
console.log(s_proto instanceof Object) // false | |
console.log(typeof s_proto === 'object') // true | |
console.log(s_proto == null) // false | |
console.log(s.valueOf() === s_proto.valueOf()); // false | |
console.log(s_proto.valueOf() === s_proto); // true | |
// this is the end of Prototype Chain itself | |
console.log(Object.getPrototypeOf(s_proto)); // null | |
const AndNull = function () {}; | |
AndNull.prototype = s_proto; | |
const z = new AndNull(); | |
console.log(Object.getPrototypeOf(z)); | |
console.log(Object.create(null)); | |
// other primitives // | |
const AndNumber = function () {}; | |
AndNull.prototype = 5; | |
const n = new AndNumber(); | |
// here ther is no primitive value | |
console.log(n); | |
console.log(Object.getPrototypeOf(n)); | |
// but here, as it is wrapped, it is OK | |
const ButAndNumber = function () {}; | |
ButAndNumber.prototype = new Number(5); | |
const nn = new ButAndNumber(); | |
console.log(nn); | |
console.log(Object.getPrototypeOf(nn)); | |
console.log(Object.getPrototypeOf(nn).valueOf()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment