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...
As I've wrote in last post, there's some JavaScript feature I would like to have in PHP too. This time we will use a basic implementation of JavaScript Object constructor in PHP. What we need to start is this class, based on SPL ArrayAccess interface. class Object extends stdClass implements ArrayAccess { // (C) Andrea Giammarchi - webreflection.blogspot.com - Mit Style License // static public methods static public function create(){ return new Object; } static public function parseJSON($json){ return self::create()->extend(json_decode($json)); } static public function parseSource($source){ return self::create()->extend(unserialize($source)); } // basic JavaScript like methods public function extend(){ for($i = 0, $length = count($arguments = func_get_args()); $i foreach($arguments[$i] as $key => $value) $this->$key = $value; return $th...
Comments
Post a Comment