4 ms·
That is simply not true, io language is prototype based yet it is simple to reason about, fix for JavaScript: Object = Object.prototype Function = Func
by sergeykish 6y ago
That is simply not true, io language is prototype based yet it is simple to reason about, fix for JavaScript:
Object = Object.prototype
Function = Function.prototype
That's how most dynamic languages work, no need for explanations, obvious assertions:
Object.__proto__ === null
Function.__proto__ === Object
object = new Object.constructor
object.__proto__ === Object
object.constructor === Object.constructor
object.toString === Object.toString
object.private = function () { return 'hello' }
Object.shared = function () { return 'world' }
Object.constructor.static = function () { return '!' }
It shows hidden complexity — what does instanceof mean?
// Object instanceof Function
Object.constructor.__proto__.constructor === Function.constructor
// Object instanceof Object
Object.constructor.__proto__.__proto__.constructor === Object.constructor
// Function instanceof Function
Function.constructor.__proto__.constructor === Function.constructor
// Function instanceof Object
Function.constructor.__proto__.__proto__.constructor === Object.constructor
Why would anyone want this? Lets get rid of .constructor on both sides:
Object.constructor.__proto__ === Function
Object.constructor.__proto__.__proto__ === Object
Function.constructor.__proto__ === Function
Function.constructor.__proto__.__proto__ === Object
Now it is obvious — constructor is a Function, Functions __proto__ is Object. JavaScript programmers jump through ".prototype" hoops which should not be exposed.
- pwdisswordfish4 6y ago... what? This comment is incomprehensible. What does it have to do with the parent comment (besides exemplifying the "teenager left at home for the first time" and creating-a-mess stuff)? None of this crazy reflective, meta-level programming belongs in the LOB application logic of any program. Save it for your programmer workspace/REPL environment--which is something that 90+% of JS programmers aren't using anyway. They're all editing "dead" files in Visual Studio Code and SublimeText. > Object.constructor.__proto__.constructor === Function.constructor // Object instanceof Object ... and that's not even what instanceof means.
- sergeykish 6y agoDo you ever type "prototype"? Do you ever type "prototype" in any language but JavaScript? How it differs from other languages accessing [[Prototype]] / class directly, not through constructor? I've thought that's caused by prototype inheritance, but no io language [1] prototype based yet easy to understand. The only difference — it does not hide [[Prototype]]. I've redefined Object and Function to show how it works. You would get same result in "dead" files. REPL to make it easy to reproduce. Imagine it is another language object.private = function () { return 'hello' } Object.shared = function () { return 'world' } Object.constructor.static = function () { return '!' } vs object.private = function() { return 'hello' } Object.prototype.shared = function () { return 'world' } Object.static = function () { return '!' } Can you see the difference? Or straight from io tutorial Contact = (class { constructor(name, address, city) { this.name = name this.address = address this.city = city } }).prototype holmes = new Contact.constructor("Holmes", "221B Baker St", "London") Contact.fullAddress = function() { return [this.name, this.address, this.city].join('\n') } holmes.fullAddress() No "prototype" (except from class but it can be redefined). > ... and that's not even what instanceof means. Thank you, it should check [[Prototype]] not constructor // Object instanceof Object Object.constructor.__proto__ === Function // Object instanceof Object Object.constructor.__proto__.__proto__ === Object // Function instanceof Function Function.constructor.__proto__ === Function // Function instanceof Object Function.constructor.__proto__.__proto__ === Object [1] https://iolanguage.org/ https://iolanguage.org/
- pwdisswordfish4 6y ago... what? > Do you ever type "prototype"? Not every day or even once in an average week, unless you're doing something wrong. > You would get same result in "dead" files. You don't understand. You shouldn't even be futzing with these things in _any_ file in the first place. Keep this sort of thing inside your REPL, and keep it out of anything checked into source control or anything distributed to other other people. 99% of ordinary LOB logic simply does not require (or justify) the use of these kinds of meta-programming facilities. Don't start using clever code reflection tricks unless you're writing a reflection framework. Dropping this highly relevant link here for the second time: https://users.dcc.uchile.cl/~rrobbes/p/EMSE-features.pdf https://users.dcc.uchile.cl/~rrobbes/p/EMSE-features.pdf > straight from io tutorial `Contact = [...]` Er... okay. In JS: class Contact { constructor(name, address, city) { this.name = name; this.address = address; this.city = city; } getFullAddress() { return this.name + "\n" + this.address + "\n" + this.city; } } let holmes = new Contact("Holmes", "221B Baker St", "London"); console.log(holmes.getFullAddress()); Prints: Holmes 221B Baker St London