JAVASCRIPT ARRAY扩展

function XHeir(a, b) {
for (var c in b) {
if (typeof (b[c]) == "function") {
a[c] = b[c];
}
}
return a;
}
function xArray(ar) {
return XHeir((ar || new Array()), xArray._property);
}
xArray._property = {
find:function (pattern) {
for (var i = this.length-1; i>=0; i--) {
if (pattern == this[i]) {
return i;
}
}
return -1;
},
insertAt:function (ind, value) {
                  ind = ind?ind:this.length;
this[this.length] = null;
for (var i = this.length - 1; i >= ind; i--) {
this[i] = this[i - 1];
}
this[ind] = value;
},
removeAt:function (ind) {
this.splice(ind, 1);
},
swapItems:function (ind1, ind2) {
var tmp = this[ind1];
this[ind1] = this[ind2];
this[ind2] = tmp;
}
};
这个是从DHTMLXGRID中拿出来的一段代码。


function dhtmlXGridComboObject() {
this.keys = new dhtmlxArray();
this.values = new dhtmlxArray();
this.put = function (key, value) {
for (var i = 0; i < this.keys.length; i++) {
if (this.keys[i] == key) {
this.values[i] = value;
return true;
}
}
this.values[this.values.length] = value;
this.keys[this.keys.length] = key;
};
this.get = function (key) {
for (var i = 0; i < this.keys.length; i++) {
if (this.keys[i] == key) {
return this.values[i];
}
}
return null;
};
this.clear = function () {
this.keys = new dhtmlxArray();
this.values = new dhtmlxArray();
};
this.remove = function (key) {
for (var i = 0; i < this.keys.length; i++) {
if (this.keys[i] == key) {
this.keys._dhx_removeAt(i);
this.values._dhx_removeAt(i);
return true;
}
}
};
this.size = function () {
var j = 0;
for (var i = 0; i < this.keys.length; i++) {
if (this.keys[i] != null) {
j++;
}
}
return j;
};
this.getKeys = function () {
var keyAr = new Array(0);
for (var i = 0; i < this.keys.length; i++) {
if (this.keys[i] != null) {
keyAr[keyAr.length] = this.keys[i];
}
}
return keyAr;
};
this.save = function () {
this._save = new Array();
for (var i = 0; i < this.keys.length; i++) {
this._save[i] = [this.keys[i], this.values[i]];
}
};
this.restore = function () {
if (this._save) {
this.keys[i] = new Array();
this.values[i] = new Array();
for (var i = 0; i < this._save.length; i++) {
this.keys[i] = this._save[i][0];
this.values[i] = this._save[i][1];
}
}
};
return this;
}
function Hashtable() {
this.keys = new dhtmlxArray();
this.values = new dhtmlxArray();
return this;
}
Hashtable.prototype = new dhtmlXGridComboObject;
看了这段之后,我才觉得我就仅仅知道JS中继承的定义。DTHMLXGRID用继承的方法实现了JS中属性和方法合理定义,清晰明了。

你可能感兴趣的:(JavaScript,C++,c,C#,prototype)