5 ms·
I cannot agree that OOP in Javascript is anywhere near good. Every library (like Backbone) includes a layer to emulate traditional Java-like classes with privat
by codedokode 10y ago
I cannot agree that OOP in Javascript is anywhere near good. Every library (like Backbone) includes a layer to emulate traditional Java-like classes with private/public fields and inheritance. Because until ES5 there were no syntax for classes, and in ES5 it is just a syntax for adding methods to a prototype and not a real class.
And JS is too forgiving, even more forgiving than PHP. In JS you can misspell object fields's name and learn about it only when undefined value will cause an error somewhere in another module.
- qwertyuiop924 10y agoThat's because many dislike prototypical inheritance. It's perfectly usable. As for misspelling field names, the same is true for variable names in both languages, and many things in many languages. Here it's more justified, as JS semantics mean that the language can't know your intent in this case: check your spelling.
- jstimpfle 10y agoIn fact what's the difference between prototypical and conventional inheritances? I think python has exactly the inheritance model of JavaScript, and statically typed languages differ only in that they make the class=type, object=value distinction (so you can't dynamically change classes).
- qwertyuiop924 10y agoIt's not. In python, classes are entities like any other object. In JS, classes literally don't exist. Yes, even in ES6. I know there's a class keyword, but it doesn't actually create a class. What the class keyword creates is a constructor function which then initializes new objects with a set of properties, and also sets a prototype, when called with the new keyword. JS objects don't have a superclass, they have a prototype. A prototype isn't a class: it's another object. When a method is called, or a variable is looked up on a JS object, JS scans up the prototype chain, all the way to Object.protype. But there's nothing special about these objects. If you wrote, say: var Person = function(){}; Person.prototype = {name:"bill", addr:"foo"}; var charlie = new Person(); charlie.name = "charlie"; charlie.addr = "baz"; var NewPerson = function(){}; NewPerson.prototype = charlie; var bill = new NewPerson(); This code is totally valid. bill's prototype chain looks like this: bill -> charlie (or NewPerson.prototype) -> Person.prototype -> ... -> Object.prototype Welcome to prototypical inheritance.
- deathanatos 10y ago> A prototype isn't a class: it's another object. When a method is called, or a variable is looked up on a JS object, JS scans up the prototype chain, all the way to Object.protype. A type in Python is also just another object. When a method is called, or a variable is looked up on an object, Python scans the MRO (effectively the same thing as the prototype chain, except it supports multiple inheritance too) all the way up to `object`. Python's type system is very similar, if not nearly a superset (though probably not exactly), of JavaScript's. I struggle to see the real material differences in them. Consider your example, translated: class Person: name = 'bill' addr = 'foo' class Charlie(Person): name = 'charlie' addr = 'baz' class NewPerson(Charlie): pass bill = NewPerson()
- qwertyuiop924 10y agoThat looks the same, but conceptually (and actually, code-wise), that's not what JS is doing. Here's what's actually happening, in psudo-python: class Person({name:"bill", addr:"foo"}): #there's no syntax for object literals in Python #such a thing doesn't even make sense: so I'm improvising. pass charlie = Person() charlie.name = "charlie" charlie.addr = "baz" class NewPerson(charlie): #note charlie is an object, not a class pass bill = NewPerson() In JS, charlie isn't a class, charlie is an object: you can change all its properties and everything, just like any other object. It's not a class, because there aren't any.
- OOPMan 10y agoI notice you conveniently ignored the meta-programming capabilities of Python which would allow your "nonsensical" improvisation to work just fine. Brush up on your Python skills before talking rubbish.
- qwertyuiop924 10y agoClassless object literals actually don't make sense in the Python model. It seems like you're the one who needs to brush up. In any case you're not actually providing any links or facts, so unless I dig through long articles on metaclasses in python, I can't verify anything you say. Please prove your point, or at least provide I link so I can verify.
- codedokode 10y ago> As for misspelling field names, the same is true for variable names in both languages, and many things in many languages. No. This code causes an error in PHP: $name = 'John'; echo $nmea; // error class A { public $name; } $a = new A; echo $a->nmea; // error This code doesn't raise an error in JS: var a = { name: 'John'; }; doSomething(a.nmea); // no error So with JS you learn about errors later and spend more time on debugging.
- hiou 10y agoActually it throws an E_NOTICE. Something a fair amount of projects require you to turn off. You know what else throws an E_NOTICE? $a = notice_there_are_no_quotes; echo $a; // $a gets assigned the string 'notice_there_are_not_quotes' This is super fun when you attempt to use a constant and misspell it. $b = MY_CONSTANTT; echo $b; // $b is now the string 'MY_CONSTANTT' instead of whatever the correct MY_CONSTANT was set to. Which is why in both PHP and JavaScript if you really cared about what you were doing, both of those would be methods that throw errors if misspelled let a = new A(); a.setName('John'); doSomething(a.getNmea()); // Error $a = new A(); $a->setName('John'); doSomething($a->getNmea()); // Also error
- codedokode 10y ago> Something a fair amount of projects require you to turn off. You know what else throws an E_NOTICE? That is true only for low quality legacy code. I have always developed with all kinds of errors enabled and converted to exceptions (that means single E_NOTICE terminates the program with exception as it is done in Java). Modern frameworks like Symfony 3 or Slim use the same approach. If you write a project using Symfony you won't miss a notice. I think default PHP error handling system is wrong - there should be no warnings or notices, only exceptions. Sadly OOP was not very popular among PHP devs in the beginning.
- arenaninja 10y agoIIRC PHP3 didn't even have objects PHP4 introduced objects and is the language people shit on when they say PHP doesn't have real OOP PHP5 introduced Java-like objects PHP5.3 introduced namespaces
- josteink 10y ago> In JS you can misspell object fields's name and learn about it only when undefined value will cause an error somewhere in another module. You should try "use strict" sometime. If you write sloppy JS, you'll find out in no time. Javascript will still have most of its warts, mind you. But some of the most obvious errors should at least be catchable.
- codedokode 10y agoIt doesn't help to find a misspelled property (and doesn't prevent you from dividing by zero too). You need a static typing system like TypeScript to detect such kind of bugs.