Posts

JavaStrict Act III - Documentation and official 1.0 Release

My JavaScript Strict Type AS 2.0 like proposal now has a dedicated documentation page with some example code for each function or public static method too. The big limit in this " IE yet loosing users but persists " era is that native DOM nodes aren't usable as return value or arguments on cross-browser libraries, " just " for FireFox, Safari or Opera, where each node has its specified constructor and where Window is a valid constructor too. I'm yet waiting for your feedbacks, expecially from some Dojo Toolkit developer ... shouldn't Strict function be useful to write better code removing a lot of comments from your unpacked sources? I hope so :-) Update 2007-05-27 Alex Russell asked me a better example to understand why JavaStrict should be a good Dojo implementation. This is just a quick and simple example on how src/json.js file should be: dojo.json = { jsonRegistry: new dojo.AdapterRegistry(), register:Strict(Void, function(name, check, wrap...

JavaStrict Act II - AS2.0 like Strict Type with JavaScript

I've just updated my JavaStrict public, static, function. It has been successfully tested with different cases and now it behaviour is like ActionScript 2.0 With this function , and dedicated Strict shortcut one, You can: define a return type, using a specific constructor or Void , if function mustn't return any kind of variable (null too) define one or more argument constructor, using specified constructor or Void if function mustn't accept any kind of parameter (null too) use a generic Object constructor, as returned or accepted instance if You don't know what kind of object function, r method, will accpet or return You can use Object for DOM elements too, expecially with Internet Explorer (6 or lower) where DOM Elements are typeof "object" but aren't instances of Object. Object is the super incoming or outgoing value type, it can be null or every kind of instance (obviously, Void is not Object, so You can't use Object to return Void values and vice-...

JavaStrict - Strict Type arguments / returned values with JavaScript

I've just uploaded on devpro my last experimental (stable) function, called JavaStrict. What is JavaStrict goal? JavaStrict allows developers to add Strict Type in every kind of function or method, with or without prototype. Strict Type should be used for function arguments or for its returned value. JavaStrict static methods just create a function wrapper that will check automatically if arguments or returnValue are exactly specified instances. How does JavaStrict work? JavaStrict function has 3 public static methods: apply , to set Type of each accepted argument using an array as second one call , to set one or more valid Type manually returnValue , to set a valid returned value Type Examples // showMe is a basic function function showMe(str){ alert(str); }; // I need that single argument is a string instance // (new String("abc") or "abc" works correctly) showMe = JavaStrict.call(showMe, String); // test them try { showMe("Hello Strict!"); // Ok ...

Joking with JavaScript constructors

This object is Rhino on Cocoon Flow.js Ready (if it's configured to don't use code evaluation using eval), have fun with crazy inheritances and constructors :) var Instance = { /* Description: creates a new instance of specified constructor using second parameter as constructr arguments. This is just a New method alias. Example: var str = Instance.Create(String, ["Hello World"]); Arguments: Function instance constructor Array constructor arguments or empty array Returns: Object new instance of specified constructor */ Create:function(){ return this.New.apply(this, arguments); }, /* Description: creates a new instance of last specified constructor and extends them with every precedent constructor, if present. Each constructor need to have a dedicated array (empty too) to use them as its arguments. Example: function String2(){ this.trim = function(){ return this.replace(/^\s+|\s+$/g, ""); }; }; var str = Ins...

JavaScript private Python style properties - yet another experiment

JavaScript constructors (call them classses if You prefere) have some problem with private parameters or private methods. To use a private parameter You should simply use a variable without this as scope prefix function MyConstructor(){ this.getPrivate = function(){ return private; }; var private = "something"; }; It's simple and it works perfectly. Any external scope could get directly private variable and You can know its value only calling getPrivate method. var obj = new MyConstructor; alert(obj.getPrivate()); The cool thing is that if You change externally returned value it will not be changed inside object too but please remember that if You change them and it's a generic object or an array, these changes will be valid inside object scope too (object and arrays are sent by reference, You need to clone or copy them before to return them externally). Well, this is just a private parameters introduction and this is not useful if You write only using prototype ...

JSON and JSONRequest

I've just updated last JSON JavaScript object adding a dependency on JSONError dedicated error object. I've uploaded a new object too, called JSONRequest and based on Douglas Crockford proposal . I successful tested both JSON and JSONRequest but last one probably need more debug to work perfectly. JSONRequest object allows developer to do more than a single JSON interaction using a queue variable to block multiple request errors and to reduce server requests, performing each request only if precedent one is completed. The usage is very simple and if You read "official" JSONRequest page You'll agree that everything was implemented, except for server specs. Server side should check Content-Type, with this object always setted as application/jsonrequest. For POST requests server side will recieve a JSONRequest key that will contain JSON Array or Object rappresentation encoded using encodeURIComponent. Compatibility should be good enought, starting with IE 5.5 and ...

Working on eval solution ...

Sorry guys, I'm trying to solve the problem but it seems really hard to do. I found a new way but I tested this piece of code: Function.prototype.constructor = function(){ sendToMaliciousCode.apply(null, arguments); return function(){}; }; and I noticed that it changes even (function(){}).constructor ... that's why constructor is not read only and it can't be deleted. I hope I'll find a solution as soon as I can ... and please sorry for fakes solutions. Update I don't know if this piece of code should be useful or should resolve the problem ... I'm testing them but I can't crack them. function safeEval(c){ var f = function(){}, m = "constructor"; if(new f[m]() instanceof f[m]) return new f[m]("", "return "+c)() }; alert( (function(c,f,m,u){f=function(){},m="constructor";return new f[m]() instanceof f[m]?new f[m]("","return "+c)():u}) ("[1,2,3]") ); This is a bit bigger than old v...