Posts

Showing posts with the label unobtrusive

JavaScript Relator Object, aka unobtrusive informations

Have you never though about add, modify, or remove a generic information from a variable, and without changing variable itself? This is all about what my last simple creation does. Relator Object Concept The main purpose for this object is to associate every kind of information, value, or function, into a generic variable, whatever it is, and without modifying its native state. To obtain this result, I have used a 1:1 relationship between a stack that will contain every stored variable, and another one that will contain related objects. Stack = [1, 2, 3]; RelatedObject = [{}, {}, {}]; // add a property RelatedObject[Stack.indexOf(2)].description = "Number 2"; // remove a property Stack.splice(0, 1); RelatedObject.splice(0, 1); // situation Stack = [2, 3]; RelatedObject = [{description:"Number 2"}, {}]; Using above strategy we obtain 2 benefits: The Stack does not cause memory leaks both Stack and RelatedObject are alw...

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