Do they belong to you? Claim these comments.
kangax
Is this you? Claim Profile »
2 months ago
in Understanding the JavaScript new keyword on trephine.org
I wouldn't create an extra Array object here and invoke a somewhat expensive `Array.prototype.shift`. `slice` usually does the job much more efficiently:
function factory(fn) {
var obj = { };
fn.apply(obj, Array.prototype.slice.call(arguments, 1));
obj.constructor = fn;
return obj;
}
A similar "pattern" is useful when one needs to invoke a constructor with arguments as an array, sort of combining `new` and `apply` (it would be trivial to `apply` constructor if `new` was a method, not an operator, of course):
function newApply(fn, args) {
function F(){};
F.prototype = fn.prototype;
var o = new F();
fn.apply(o, args);
o.constructor = fn;
return o;
}
or a slightly more efficient version which doesn't recreate a "proxy" Function object every time `newApply` is called (instead keeping it in a closure) -
var newApply = (function(){
function F(){};
return function(fn, args) {
F.prototype = fn.prototype;
var o = new F();
fn.apply(o, args);
o.constructor = fn;
return o;
}
})();
function factory(fn) {
var obj = { };
fn.apply(obj, Array.prototype.slice.call(arguments, 1));
obj.constructor = fn;
return obj;
}
A similar "pattern" is useful when one needs to invoke a constructor with arguments as an array, sort of combining `new` and `apply` (it would be trivial to `apply` constructor if `new` was a method, not an operator, of course):
function newApply(fn, args) {
function F(){};
F.prototype = fn.prototype;
var o = new F();
fn.apply(o, args);
o.constructor = fn;
return o;
}
or a slightly more efficient version which doesn't recreate a "proxy" Function object every time `newApply` is called (instead keeping it in a closure) -
var newApply = (function(){
function F(){};
return function(fn, args) {
F.prototype = fn.prototype;
var o = new F();
fn.apply(o, args);
o.constructor = fn;
return o;
}
})();
1 year ago
in Parameters string and the Prototype Ajax.Request method on naterkane.com | the blog
Feel free to submit any bugs/suggestions to prototype's bugtracker: http://prototype.lighthouseapp.com/projects/888...
Ajax.Updater "defaults" its parameters to an empty object internally, so I'm not sure how this error could appear.
Ajax.Updater "defaults" its parameters to an empty object internally, so I'm not sure how this error could appear.
1 year ago
in Clearing the contents of a DOM Element on naterkane.com | the blog
Which memory loss are you referring to? I'm not aware of any "leaks" when clearing contents of an element by settings its innerHTML to an empty string.
1 year ago
in Self-Printing JavaScript Literals on Languages of the real and artificial
Interesting.
We could of course go even further and support n-level nesting (which is not exactly the point of the article, but is still entertaining)
[code]
function defineLiteral() {
var o = window, args = arguments, prop;
for (var i=0, l = arguments.length; i<l; ++i) {
prop = arguments[i];
o[prop] = o[prop] || { }; o = o[prop];
if (i == l-1) o['toString'] = function() {
return Array.prototype.join.call(args, '.')
}
}
return o;
}
defineLiteral('bar', 'baz', 'qux', 'quux'); // => bar.baz.qux.quux
bar.baz.qux.quux.toString(); // => 'bar.baz.qux.quux'
[/code]
We could of course go even further and support n-level nesting (which is not exactly the point of the article, but is still entertaining)
[code]
function defineLiteral() {
var o = window, args = arguments, prop;
for (var i=0, l = arguments.length; i<l; ++i) {
prop = arguments[i];
o[prop] = o[prop] || { }; o = o[prop];
if (i == l-1) o['toString'] = function() {
return Array.prototype.join.call(args, '.')
}
}
return o;
}
defineLiteral('bar', 'baz', 'qux', 'quux'); // => bar.baz.qux.quux
bar.baz.qux.quux.toString(); // => 'bar.baz.qux.quux'
[/code]