Posts

Showing posts from June, 2008

Ajax or JavaScript sub domain request

Some time we could have a situation like this one: a site in a sub domain, called http://a.test.com another site in another sub domain, called http://b.test.com of course, a generic main site, called http://test.com A common problem between one or more sub domains, is the possibility to use, or call, scripts in the main domain, because of security restrictions. The simplest solution is to force, in the client side, the document.domain, specifying the common one, i.e. dcument.domain = "test.com"; In this way you can add, for example, an iframe, and read its content, or use parent window object from the iframe that points, for example, to http://test.com There are different reasons to do it, and one of them, is the ability to share a global, or common, space, between every sub domain, performing Ajax requests or whatever else we need. This object, saved in http://test.com, and loaded by every other sub domain, like "a" or "b", could solve in a really simple ...

From the future, a PHP JavaScript like Number class, with late static binding and operator overloading

PHP and operator overloading Not everyone knows that with PHP, and since 2006, it is possible to implement operator overloading, thanks to a PECL extension . One common PHP VS other language flame, is about PHP poor OOP expressiveness and power. Another common request from skilled PHP developers, is the possibility to create a class that is able to manage every situation during code execution. For example, try to think about a Number class, and try to use every native Magic method to perform a simple task like this one: $i = new Number(123); $i += 2; echo $i; // 125 Above code is simply not implementable with current version of PHP 5.2 or greater, if we do not install php_operator.so or .dll. Unfortunately, this extension is still experimental, and not documented at all. That is why I have created a full class, based entirely on this extension, and for this post only, compatible with PHP 5.2 (then, without usage of late static binding). The JavaScript like Number class Next code, is a...

Lazy developers, Stack concept, and the fastest, unobtrusive, JavaScript StringBuilder

Fast Web, and lazy developers About 1 month ago, I have posted a little piece of code that, in some way, could be a little revolution for JavaScript 3rd edition, and every kind of library. I explained how to use native Array methods with an object, to extend Array itself, or simply create a Stack behaviour, compatible with every used browser. Few comments a part, in both Ajaxian and this site, it seems that nobody had enough fantasy to use that tricky piece of code to create every kind of Stack based constructor, library, or Iterator, without destroying the native Array.prototype. So, here I am, with another little piece of code that uses the same Stack concept to create a StringBuilder constructor. The Fastest Unobtrusive JavaScript StringBuilder A common problem with JavaScript and, specially, Internet Explorer, is string concatenation. You can find every kind of benchmark, even in IEBlog , about string concatenation problems. IE Team seems to suggest the usage of an Array, becaus...

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 ...

Has this constructor been prototyped?

This is a quick post about libraries that uses native constructor in an obtrusive way. Using a Function prototype, that sounds like a non-sense, it is possible to know if a constructor has some property, or method, defined in its prototype. Function.prototype.prototyped = function(){ for(var i in new this) return true; return false; }; Some test example? alert(Array.prototyped()); // false Object.prototype.each = function(){}; alert(Array.prototyped()); // true delete Object.prototype.each; alert(Array.prototyped()); // false Array.prototype.each = function(){}; alert(Array.prototyped()); // true alert(Object.prototyped()); // false Update Has Kangas spotted, there is no reason to create a new instance. We could loop directly the prototype object. But what's up if we have injected privileged methods in the constructor? Array = function(Array){ var prototype = Array.prototype; return function(){ arguments = prototype.slice.call(arguments, 0); ...

JsonTV - JsonML concept to create TreeView components

For those that do not know JsonML , this is a little summary. JsonML aim is to use JSON to transport layouts, instead of simple data. Unfortunately, for this purpose the size of the code could be even bigger than regular layout, considering that with a good usage of classes, internal styles, as attributes, are often superfluous. At the same time, there are still several problems with DOM, for its not standard nature, thanks to those browsers that do not respect W3 or generic predefined guidelines. For these reasons, and probably others, JsonML has not been widely adopted, as is for his daddy JSON . An alternative purpose, based on both same concept and grammar A daily common task in web development, is menu, files and directories, or trees creation, that allow us, as users, to organize data in a better, friendly, and easy to manage, way. Removing some intermediate step, and thinking about standard data presentation, with useful information and nothing else, it is possible to use the...

Two simple tricks in JavaScript ( olds, but always useful )

This is a quick post about two really common pieces of code that are used daily from libraries developers and not. Stop to use Math.floor The first one is about usage of Math.floor. It is probably only my opinion, but it seems that Math.floor is used always to perform the same task: var centerWidth = Math.floor((something + someelse - someother) / 2); The point is that at the end of a Math.floor, you will often find that division by 2 . There is a truly simple way to write less, and to obtain best performances as well, it is the right side bitwise operator, that for this purpose is nearly perfect. var centerWidth = (something + someelse - someother) >> 1; That's it! If you compare above examples you will note that second one is about 2X faster than Math.floor. for(var i = 0, t1 = new Date; i Math.floor(i / 2); t1 = new Date - t1; for(var i = 0, t2 = new Date; i i >> 1; t2 = new Date - t2; alert([t1, t2].join("\n")); A tricky String.indexOf Another common pie...

JavaScript prototype behaviour with PHP

One cool thing of JavaScript prototype model, is that you can change dynamically one or more method updating automatically every instance of that constructor. Even if classic inheritance and OO Programmers hate this feature, it could be really useful in some case. Another interesting thing, is that thanks to injected scope, you can share a prototype or use one of its defined methods, with every kind of instance. PHP is (still) dynamically limited The concept of injected scope, is absolutely extraneous to PHP developers. Even with a massive usage of Reflection API , it is not possible to use a method of class A with another class B , even if this method contains common usable tasks for both classes. At the same time, it is not possible to use a function as property, because it is not recognized as function, if called directly, and it cannot contain a $this referer, thanks to engine limitation. The light comes from PECL The official repository for PHP extensions, contains a truly inte...

PHP or JavaScript implicit Factory method design pattern

Update Above technique could be used to create an implicit Singleton as well. class Demo { // your unbelievable stuff } function Demo($some, $arg){ static $instance; return isset($instance) ? $instance : ($instance = new Demo($some, $arg)); } Demo(1,2)->doStuff(); Demo(1,2) === Demo(1,2); // true from Wikipedia The factory method pattern is an object-oriented design pattern ... More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects. I have talked about JavaScript possibility different times in my prototypal inheritance documentation, and in other posts of this blog. The summary is that thanks to perfect this behaviour , that for some unknown reason someone would like to modify in JS2 , making them ambiguous when you are using a constructor as a function and not, for example, as private method, we can create intelligent constructors that does not require the usage of new keyword - in a Pythonic way: // Java...