Posts

Showing posts with the label errors

JavaScript FatalError

just in case at some point you decide to break everything, regardless possible try catches around ... function FatalError(message, file, line){ function toString() { throw e; } var e = this; e["@message"] = message; e["@file"] = file || e.file || e.fileName; e["@line"] = line || e.line || e.lineNumber; if ("__defineGetter__" in e) { e.__defineGetter__("message", toString); e.__defineGetter__("description", toString); } else { e.message = e.description = {toString: toString}; } // just in case, but not necessary e.toString = e.valueOf = toString; }; (FatalError.prototype = new Error).name = "FatalError"; how to use it? throw new FatalError("this must be a joke!"); P.S. it won't work if Errors are not logged ( silent failures, definitively a problem for certain applications ;) )

Good Old And Common JavaScript Errors

... I won't write any introduction, but I'll let you comment, OK? for loops (plus ++i) for(i = 0; i // global i defined for(var i = 0; i // cache the length if it won't change during the loop // associate once only under certain conditions to speed up for(var i = 0; i // there is absolutely nothing wrong in above loop Few people told me something like: " doode, if you use ++i at the end you need to write i " ... basically the for loop has been rarely understood, let me try again: // directly from C language, 1972 for( // optional inline declaration, coma accepted for more vars ; // optional condition to verify // if undefined, it loops until a break is encountered // this condition is performed BEFORE the first loop // if false, the loop will never be executed ; // optional POST operation, it will never be executed // if the condition is false, "", 0, or null // it is executed in any case AFTER the loop, if any ){}...

W3 HTML5 Storage - What Works, What Does Not

My last sessionStorage is basically ready to implement storage events but apparently I cannot implement them right now. Why not? Doing some test I just discovered a lot of inconsistency for each browser which supports Web Storage in core, so here I am with a little report and some suggestion. The Correct Way To Set or Get Items Every single example I've read so far about Web Storage is not respecting standards defined by the official draft , even if related browsers respect the official API. // bad sessionStorage usage example if(sessionStorage.myKey !== null){ // not implementable with old browsers // it could inherit from a modified Storage prototype // it could be a false positive with native API } else { sessionStorage.myKey = "myValue"; // requires getter/setter, improbable with other browsers // it could overwrite native properties or methods // sessionStorage.key = "value"; will *break* the object }; // W3 standard sugges...

[PHP] Strict error on static call? Solved!

Just a quick post about Strict standard error when we have a class method which would like to behave differently if called statically. Apparently, and this is a truly old bogus I opened ages ago , the solution was to supress Strict errors (lol, thanks developers) even if in every other programming language, via overload, this kind of situation could be solved in a bit. How to solve the problem? While last post I suggested a couple of runtime solutions, this time I suggest to simply avoid the method definition and use a different one with a meaningful name. In this example I created a String class: class String { // choose a recognizable name for protected method static protected function _concat(array $arguments){ return new self(implode('', $arguments)); } // use magic __callStatic function static public function __callStatic($name, array $arguments){ // check the recognizable name switch($name = '_'.$name){ ...

Famous documentation and the dark side of "this" !

The good thing of internet is that you can find a lot of free documentation. At the same time, the bad thing of internet, is that this documentation is rarely updated. It could be a "guru documentation" or it could be a newbie documentation, but in both cases, it doesn't matter because it is probably wrong, not updated, or too generic. This post has not enough space to describe every error you can find on, and off line ... so let me start with some true example about a common misunderstood referer, the this one. Douglas Crockford and Private Members in JavaScript This page is famous enough, and I suppose that every true JavaScript developer has read them at least once. The main error in that page is described in this sentence By convention, we make a private that parameter. This is used to make the object available to the private methods. This is a workaround for an error in the ECMAScript Language Specification which causes this to be set incorrectly for inner functio...