Pointers And Array Class In Javascript
Solution 1:
Maybe I'm oversimplifying, but why not just add the extra methods you need to the Array prototype and call it directly?
Solution 2:
I need to make a class that has mostly the same function as a given array class unshift,shift,push and pop but with different names.
I suppose you could add these "new" methods to Array.prototype
.
Solution 3:
Like this perhaps?
var makeDeque = (function (ap) {
varDeque = {
length: 0,
pushHead: ap.unshift,
popHead: ap.shift,
pushTail: ap.push,
popTail: ap.pop,
isEmpty: function () {
return !this.length;
}
};
returnfunction () {
returnObject.create(Deque);
};
})(Array.prototype);
If it's still too long, you can always directly augment Array.prototype
like others already mentionned. We agree that it's all experimental here and the only goal is to save characters.
!function (ap) {
ap.pushHead = ap.unshift;
ap.popHead = ap.shift;
ap.pushTail = ap.push;
ap.popTail = ap.pop;
ap.isEmpty = function () {
return !this.length;
};
}(Array.prototype);
functionmakeDeque() {
return [];
}
This can be compressed to 174 chars:
functionmakeDeque(){return[]}!function(e){e.pushHead=e.unshift;e.popHead=e.shift;e.pushTail=e.push;e.popTail=e.pop;e.isEmpty=function(){return!this.length}}(Array.prototype)
Solution 4:
Not sure why you need this, but my suggestions per best practice are:
- Don't override the
Array.prototype
. The reason for this is because other libraries might try to do the same, and if you include these libraries into yours, there will be conflicts. - This code is not needed.
var a= [], r=new Array(a);
. You only need ...a = [];
. - Ensure you are creating a real class. In your code,
makeDeque
is not doing what you want. It is returningthis
which when a function is not called with thenew
keyword will be the same as thewindow
object (orundefined
if you are using what is called as "strict mode"). In other words, you have made a lot of globals (which are usually a no-no, as they can conflict with other code too). - When you build a class, it is good to add to the
prototype
of your custom class. This is because the methods will only be built into memory one time and will be shared by all such objects.
So I would first refactor into something like this:
var makeDeque = (function() { // We don't need this wrapper in this case, as we don't have static properties, but I've kept it here since we do want to encapsulate variables in my example below this one (and sometimes you do need static properties).
function makeDeque () {
if (!(this instanceof makeDeque)) { // This block allows you to call makeDeque without using the "new" keyword (we will do it for the person using makeDeque)return new makeDeque();
}
this.r = [];
this.length = 0;
}
makeDeque.prototype.setLength = function () {
returnthis.length = this.r.length;
};
makeDeque.prototype.pushHead=function(v) {
this.r.unshift(v);
this.setLength();
};
makeDeque.prototype.popHead=function() {
returnthis.r.shift();
this.setLength();
};
makeDeque.prototype.pushTail=function(v){
this.r.push(v);
this.setLength();
};
makeDeque.prototype.popTail=function() {
returnthis.r.pop();
this.setLength();
};
makeDeque.prototype.isEmpty=function() {
returnthis.r.length === 0;
};
return makeDeque;
}());
Now you could shorten this as follows, but I wouldn't recommend doing this, since, as it was well said by Donald Knuth, "premature optimization is the root of all evil". If you try to shorten your code, it may make it inflexible.
var makeDeque = (function() {
functionmakeDeque () {
if (!(thisinstanceof makeDeque)) {
returnnewmakeDeque();
}
this.r = [];
this.length = 0;
}
makeDeque.prototype.setLength = function () {
returnthis.length = this.r.length;
};
for (var i=0, methodArray = [
['pushHead', 'unshift'], ['popHead', 'shift'], ['pushTail', 'push'], ['popTail', 'pop']
]; i < methodArray.length; i++) {
makeDeque.prototype[methodArray[i][0]] = (function (i) { // We need to make a function and immediately pass in 'i' here because otherwise, the 'i' inside this function will end up being set to the value of 'i' after it ends this loop as opposed to the 'i' which varies with each loop. This is a common "gotcha" of JavaScriptreturnfunction () {
var ret = this.r[methodArray[i][1]].apply(this.r, arguments);
this.setLength();
return ret;
};
}(i));
}
makeDeque.prototype.isEmpty=function() {
returnthis.r.length === 0;
};
return makeDeque;
}());
If you need to get the length by a length
property, as opposed to a method like setLength() which sets it manually after each update, either of the above code samples could be shortened by avoiding the setLength() method, but you'd need to use the Object.defineProperty
which does not work (or does not work fully) in older browsers like IE < 9.
Post a Comment for "Pointers And Array Class In Javascript"