patternjavascriptModerate
Basic JavaScript library
Viewed 0 times
javascriptlibrarybasic
Problem
My own CMS is currently using jQuery, but as one of the goals is to have the whole project to be very small, I've decided to write my own basic library. I only really need to select elements and modify them using results from my server (via Ajax).
The JavaScript-library v0.01: (Attempt 1)
I know that
The JavaScript-library v0.02: (Attempt 2)
I believe this is much improved. It has reasonable variable names, works better, and most of all is quite buggy.
```
var _ = new function Sample(){}; ////////////////////////////////////////
_.temp = {}; // This Object is intended just for
_.pages = {}; // testing, I do the rest in the console
_.pid = {}; // <- Stores the current pageID
_.el = {}; // /PAGE-ID/PAGE-TITLE or #!page=PAGE-ID
_.fn = {}; ////////////////////////////////////////
_.$ = {}; // <- Lets leave JQuery alown. :)
(function(){
this.$ = function(a,b,c){
if(b&&b.isType("string")) b = $(b);
The JavaScript-library v0.01: (Attempt 1)
(function(){
a=this.Function;
a.prototype.extend=(function(a,b){this[a]=b;return this});
a.prototype.implement=(function(a,b){this.prototype[a]=b;return this});
$=(function(a,b,c){return (b?$(b)[c?c:0]:document).querySelectorAll(a)})
.extend("post",(function(a,b){
c=[];
for(x in a)
c[c.length]=[x,a[x]].join("=");
d=XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
d.open("POST","./",true);
d.setRequestHeader("Content-type","application/x-www-form-urlencoded");
d.onreadystatechange=b;
d.send(c.join("&"));
return this;
}))
.extend("each",(function(a,b,c){
for(x in a)
if(a.hasOwnProperty(x))
b.call(c, a[x], x, a);
return this;
}));
})();I know that
querySelectorAll can't be relied on, but it is just good for the start of this.The JavaScript-library v0.02: (Attempt 2)
I believe this is much improved. It has reasonable variable names, works better, and most of all is quite buggy.
```
var _ = new function Sample(){}; ////////////////////////////////////////
_.temp = {}; // This Object is intended just for
_.pages = {}; // testing, I do the rest in the console
_.pid = {}; // <- Stores the current pageID
_.el = {}; // /PAGE-ID/PAGE-TITLE or #!page=PAGE-ID
_.fn = {}; ////////////////////////////////////////
_.$ = {}; // <- Lets leave JQuery alown. :)
(function(){
this.$ = function(a,b,c){
if(b&&b.isType("string")) b = $(b);
Solution
Rewrite it from scratch, if I were to encounter such code in any commit, I would immediately remove that code and talk with the one who committed it about JS in general...
EDIT
An untested cleaned up version from me.
// anonymous wrapper if fine...
(function(){
// but not using var still creates global variables
a=this.Function; // why are you extending the builtin function constructor?
// no need for extra parenthesis here
// also, this is hardly needed.. for the code below
a.prototype.extend= (function(a,b){this[a]=b;return this});
// what is this being used for?
a.prototype.implement=(function(a,b){this.prototype[a]=b;return this});
// overrides an existing mapping of jQuery
$=(function(a,b,c){return (b?$(b)[c?c:0]:document).querySelectorAll(a)})
// hard to read
.extend("post",(function(a,b){
// more leakage
c=[];
// if you don't use {} you might as well put it on the same line
// Oh and I shouldn't forget my usual rant about the missing
// hasOwnProperty call
for(x in a) // leakage of x
c[c.length]=[x,a[x]].join("="); // what about c.push() ?
// leakage leakage leakage
d=XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
d.open("POST","./",true);
d.setRequestHeader("Content-type","application/x-www-form-urlencoded");
d.onreadystatechange=b;
d.send(c.join("&"));
return this;
}))
.extend("each",(function(a,b,c){
for(x in a) // leakage of x
if(a.hasOwnProperty(x)) // why all of a sudden hasOwnProperty here?
b.call(c, a[x], x, a);
return this;
}));
})();EDIT
An untested cleaned up version from me.
(function() {
function $(selector, parentSelector, index) {
var element = parentSelector ? $(parentSelector)[index || 0] : document;
return element.querySelectorAll(a);
};
$.extend = function(obj, props) {
for(var i in props) {
if (props.hasOwnProperty(i)) {
obj[i] = props[i];
}
}
return this;
};
$.extend($, {
post: function(data, callback) {
var params = [];
for(var i in data) {
if (data.hasOwnProperty(i)) {
// might need url encoding...
params.push([i, data[i]].join('='));
}
}
// this might need for fallbacks, check the jQuery source for that
var req;
if (XMLHttpRequest) {
req = new XMLHttpRequest();
} else {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open('post', '/', true);
// is that really cross browser these days?
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.onreadstatechange = callback;
req.send(params.join('&'));
return this;
},
each: function(obj, func, that) {
for(var i in obj) {
if (obj.hasOwnProperty(i)) {
func.call(that, obj[i], obj, i);
}
}
return this;
}
});
window.$ = $;
})();Code Snippets
// anonymous wrapper if fine...
(function(){
// but not using var still creates global variables
a=this.Function; // why are you extending the builtin function constructor?
// no need for extra parenthesis here
// also, this is hardly needed.. for the code below
a.prototype.extend= (function(a,b){this[a]=b;return this});
// what is this being used for?
a.prototype.implement=(function(a,b){this.prototype[a]=b;return this});
// overrides an existing mapping of jQuery
$=(function(a,b,c){return (b?$(b)[c?c:0]:document).querySelectorAll(a)})
// hard to read
.extend("post",(function(a,b){
// more leakage
c=[];
// if you don't use {} you might as well put it on the same line
// Oh and I shouldn't forget my usual rant about the missing
// hasOwnProperty call
for(x in a) // leakage of x
c[c.length]=[x,a[x]].join("="); // what about c.push() ?
// leakage leakage leakage
d=XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
d.open("POST","./",true);
d.setRequestHeader("Content-type","application/x-www-form-urlencoded");
d.onreadystatechange=b;
d.send(c.join("&"));
return this;
}))
.extend("each",(function(a,b,c){
for(x in a) // leakage of x
if(a.hasOwnProperty(x)) // why all of a sudden hasOwnProperty here?
b.call(c, a[x], x, a);
return this;
}));
})();(function() {
function $(selector, parentSelector, index) {
var element = parentSelector ? $(parentSelector)[index || 0] : document;
return element.querySelectorAll(a);
};
$.extend = function(obj, props) {
for(var i in props) {
if (props.hasOwnProperty(i)) {
obj[i] = props[i];
}
}
return this;
};
$.extend($, {
post: function(data, callback) {
var params = [];
for(var i in data) {
if (data.hasOwnProperty(i)) {
// might need url encoding...
params.push([i, data[i]].join('='));
}
}
// this might need for fallbacks, check the jQuery source for that
var req;
if (XMLHttpRequest) {
req = new XMLHttpRequest();
} else {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open('post', '/', true);
// is that really cross browser these days?
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.onreadstatechange = callback;
req.send(params.join('&'));
return this;
},
each: function(obj, func, that) {
for(var i in obj) {
if (obj.hasOwnProperty(i)) {
func.call(that, obj[i], obj, i);
}
}
return this;
}
});
window.$ = $;
})();Context
StackExchange Code Review Q#580, answer score: 13
Revisions (0)
No revisions yet.