Posts

Showing posts with the label parent

The JavaScript _super Bullshit

I know you already hate the title, but that's how I called one paragraph of my precedent post: Better JavaScript Classes . This post is mainly dedicated for both libraries authors, and those Classic OOP Developers that still think JavaScript should be used in a classic way. _super or parent Are Problematic! It's not just about performances, where " the magic " may need to replace, wrap, and assign runtime everything in order to make it happens, it's about inconsistencies, or infinite loops, or hard debug, or disaster prone approach as well, since as I have already said instances have nothing to do with _super/parent .... so, how are things? Thanks for asking! Real OOP Behavior It's the ABC, and nothing else, if we call a parent/super inside a method, this method will execute with a temporary self/this context. This means that the instance will be still: an instanceof its Class only that method will be executed, it is possible to call other super/parent accord...

PHP static as virtual self, what else for parent?

I find the static keyword in PHP 5.3 absolutely useful, but I instantly felt into a dilemma: what about parent? With static it is possible to refer to a generic class static property, without referring the class where the method has been declared. Here there is a simple example: class Numbers { public static $value = 0; public static function getValue(){ return static::$value; } } class One extends Number { public static $value = 1; } class Two extends Number { public static $value = 2; } $one = new One; $two = new Two; $one->getValue(); // 1 $two->getValue(); // 2 Above example shows how static behaves when an instance calls a method which uses static keyword. Whatever subclass it is, we will always obtain the defined value (if any, otherwise the inherited one) as expected. But what about parent behavior? If we use parent inside a method it is like using self, the parent keyword will always refers to the parent class, if any, of the method which conta...

My 5 cents about JavaScript Prototypal Inheritance

Few days ago I read an interesting post about Simple JavaScript Inheritance . The most hilarious thing is that prototypal inheritance is truly simple, as John wrote, but there are still a lot of developers that do not probably understand perfectly them. In this link you can find an " all in one " page about JavaScript prototypal inheritcance and classical emulation. As I wrote at the end of that page, please do not hesitate to correct me if there is something wrong, or please ask me more details if there is something that is not so clear. Sorry for my not perfect yet English, and have fun with JavaScript. (happy easter too!)