Posts

Showing posts with the label patterns

Few JavaScript Patterns

Just to be clear and once again, JavaScript can emulate: classes public and public static methods or properties private and private static methods or properties public and private constants protected methods ... you name it ... // duck typing ( maybe all you need ) var me = {name: "WebReflection"}; // basic class function Person() {} Person.prototype.getName = function () { return this.name; }; Person.prototype.setName = function (name) { this.name = name; }; // module pattern + private properties / methods function Person(_name) { function _getName() { return _name; } return { getName: function () { // redundant, example only return _getName(); }, setName: function (name) { _name = name; } }; } // private shared methods via this var Person = (function () { function Person() {} function _getName () { return this.name; } function _setName (name) { ...

Technical Reviews: Bestsellers!

Image
Just a quick one about two technical reviews out of two I have recently done for @stoyanstefanov and @cjno for these completely different books: JavaScript Patterns and Test-Driven JavaScript Development . Right now these are both Top 10 Bestsellers and trust me: other JavaScript Jedis have been involved, you won't regret these lectures! ;-)

A completely revisited Singleton and Factory design pattern for PHP and JavaScript

Singleton From Wikipedia In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. Sometimes it is generalized to systems that operate more efficiently when only one or a few objects exist. It is also considered an anti-pattern by some people, who feel that it is often used as a euphemism for global variable Basically, the last point is true. Whatever we think about Singleton, we cannot say that we do not use this pattern to have the same instance, or object, in every place of our application. The most common case scenario, is usually a database object, or a global queue, used as a stack, or something similar, like a template engine instance or a DOM / XML node. The worst thing ever, is that with languages like PHP and JavaScript, the Singleton pattern is really hard to implement correctly. Why bother with a class? In a lot ...