3 ms·
Do you ever type "prototype"? Do you ever type "prototype" in any language but JavaScript? How it differs from other languages accessing [[Prototype]] / class d
by sergeykish 6y ago
Do 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
- sergeykish 6y ago> Not every day or even once in an average week, unless you're doing something wrong. Do you have to understand it or not? There is a ton of information on the web (viewed 191k times) [1], [2], etc. Acting like it is intuitive is disingenuous. No, that's you who don't understand. I've shown particular code that is error prone in JS but is ok in other languages. That is the issue. It is not solved. Every novice gets it. It is simple to solve. Instead you claim "you should not need this", tell it 191k viewers. Yes, you know what's going on, still you claim "you don't need this". class Foo end foo = Foo.new foo.class == Foo // metaprogramming! *You shouldn't even be futzing with these things in _any_ file* Sure I know about method definition in class. Extend it (same class). It is useful technique. And please don't post twice same if you can't read response. [1] https://stackoverflow.com/questions/9959727/proto-vs-prototype-in-javascript https://stackoverflow.com/questions/9959727/proto-vs-prototy... [2] https://javascript.info/class-inheritance https://javascript.info/class-inheritance
- pwdisswordfish4 6y ago> that's you who don't understand Because your comments are incomprehensible. > I've shown particular code that is error prone in JS but is ok in other languages. It's not OK in Java. It's not OK in C. It's not OK in C++. Because you can't even do that in any of those languages. > still you claim "you don't need this" Look at the languages I listed above. Think about the program you're trying to write. Do you think it's impossible to write the program you want to write in those languages, where you are not even allowed to to do your clever tricks? Look at the underscores in the name __proto__. That's a huge, massive sign in your face that means, "If you find yourself touching this a lot, it's because you're doing something wrong." How could it be clearer? Imagine it were called __proto$do_not_use$__ instead. But you use it anyway. And then you ask, "why is this so difficult?". Where's the surprise? Think about this whole thread. You're doing something, experiencing pain, and then complaining about the pain that you feel. Yes, of course you feel the pain. Every time you mention the pain, you are actually giving an argument against yourself: it's not the right thing. So stop doing it. The fact that you got instanceof wrong is further evidence that you're not the best person to tell us which road to take to get to the party--you don't know the city. (As for the code snippet you wrote in this comment, it's not clear at all what you're even trying to say. Once again: incomprehensible.)