The JSON protocol is a de facto standard used in many different environments to transport objects, included arrays and Dates plus primitives such: strings, numbers, and booleans ... so far, so good! Since this protocol is widely adopted but it has not the power that a well known function as is the PHP serialize one has, we are often forced to remember how data has been stored, what does this data represent, and eventually convert data back if we are dealing with instances rather than native hashes. If we consider the ExtJS library architecture, we can basically have a snapshot of whatever component simply storing its configuration object. The type will eventually tell us how to retrieve a copy of the original component back and without effort at all. Since JSON is the preferred protocol to store data in WebStorages, databases, files, etc etc, and since we can save basically only hashes, loosing every other property, I have decided to try this experiment able to bring some magic in the...
You might think this must be a joke, well no , this is a partial lie behind the "use strict"; directive. Roots Of The Hack // global "use strict"; function strict() { "use strict"; // top of the function return { // invoked inline withStrict: function(){ return this; // undefined }(), // invoked inline too withoutStrict: Function("return this")() }; } // the test var really = strict(); really.withStrict; // undefined really.withoutStrict; // global, BOOOOM! The Good News I have been blaming since ever the fact that use strict makes impossible to retrieve the real global object ensuring nobody in the closure redefined window or global by accident so that code is more reliable. Well, now we have the possibility to return it again when it's needed for security reasons or to be sure is the right one. // a classic code for Rhino, node, and Web var G = typeof window !== "undefined" ? window : global...
Comments
Post a Comment