这是一种合理的';子类';javascript数组?
我意识到,严格地说,这不是数组类型的子类化,但这是否会以人们期望的方式工作,或者我仍然会遇到一些关于.length之类的问题?如果正常的子类化是一种选择,我会有什么缺点吗这是一种合理的';子类';javascript数组?,javascript,arrays,subclass,Javascript,Arrays,Subclass,我意识到,严格地说,这不是数组类型的子类化,但这是否会以人们期望的方式工作,或者我仍然会遇到一些关于.length之类的问题?如果正常的子类化是一种选择,我会有什么缺点吗 function Vector() { var vector = []; vector.sum = function() { sum = 0.0; for(i
function Vector()
{
var vector = [];
vector.sum = function()
{
sum = 0.0;
for(i = 0; i < this.length; i++)
{
sum += this[i];
}
return sum;
}
return vector;
}
v = Vector();
v.push(1); v.push(2);
console.log(v.sum());
函数向量()
{
var向量=[];
vector.sum=函数()
{
总和=0.0;
对于(i=0;i
EDIT——我最初写道,可以像任何其他对象一样对数组进行子类化,这是错误的。每天学习新的东西。这是一个很好的讨论
在这种情况下,作文会更好吗?i、 只需创建一个向量对象,并用数组作为它的后盾。这似乎就是您所处的路径,您只需将推送和任何其他方法添加到原型中。我会将数组包装在适当的向量类型中,如下所示:
window.Vector = function Vector() {
this.data = [];
}
Vector.prototype.push = function push() {
Array.prototype.push.apply(this.data, arguments);
}
Vector.prototype.sum = function sum() {
for(var i = 0, s=0.0, len=this.data.length; i < len; s += this.data[i++]);
return s;
}
var vector1 = new Vector();
vector1.push(1); vector1.push(2);
console.log(vector1.sum());
window.Vector=函数向量(){
这个.data=[];
}
Vector.prototype.push=函数push(){
Array.prototype.push.apply(this.data,arguments);
}
Vector.prototype.sum=函数sum(){
对于(var i=0,s=0.0,len=this.data.length;i
或者,您可以在数组上构建新的原型函数,然后只使用普通数组
如果你对数组的命名是一致的,因此它们都以小写的v开头,例如,或者类似的东西,清楚地将它们标记为aw vector而不是normal数组,并且你对特定于vector的原型函数也这样做,那么应该很容易跟踪它们
Array.prototype.vSum = function vSum() {
for(var i = 0, s=0.0, len=this.length; i < len; s += this[i++]);
return s;
}
var vector1 = [];
vector1.push(1); vector1.push(2);
console.log(vector1.vSum());
Array.prototype.vSum=函数vSum(){
对于(var i=0,s=0.0,len=this.length;i
@hvgotcodes答案有一个。我只是想总结一下结论
这似乎是本文中扩展数组的最佳方法
包装纸可以用来。。。其中,对象的原型链被扩充,而不是对象本身
不幸的是,对于我来说,这个方法使用了arr.\uuuuu proto\uuuuuuu
,在IE 8中不受支持,我必须支持这个浏览器
此方法比上述方法稍慢,但在IE8-中有效
包装器方法避免设置继承或模拟长度/索引关系。相反,类似工厂的函数可以创建普通数组对象,然后直接使用任何自定义方法对其进行扩充。因为返回的对象是数组对象,所以它保持适当的长度/索引关系,以及“Array”的[[Class]]。当然,它也继承了Array.prototype
这只是包装器的另一个例子。玩得开心点。绑定
var _Array = function _Array() {
if ( !( this instanceof _Array ) ) {
return new _Array();
};
};
_Array.prototype.push = function() {
var apContextBound = Array.prototype.push,
pushItAgainst = Function.prototype.apply.bind( apContextBound );
pushItAgainst( this, arguments );
};
_Array.prototype.pushPushItRealGood = function() {
var apContextBound = Array.prototype.push,
pushItAgainst = Function.prototype.apply.bind( apContextBound );
pushItAgainst( this, arguments );
};
_Array.prototype.typeof = (function() { return ( Object.prototype.toString.call( [] ) ); }());
有一种方式看起来和感觉上都像是原型继承,但只有一种方式不同 首先,让我们看一看在javascript中实现原型继承的方法之一:
var MyClass = function(bar){
this.foo = bar;
};
MyClass.prototype.awesomeMethod = function(){
alert("I'm awesome")
};
// extends MyClass
var MySubClass = function(bar){
MyClass.call(this, bar); // <- call super constructor
}
// which happens here
MySubClass.prototype = Object.create(MyClass.prototype); // prototype object with MyClass as its prototype
// allows us to still walk up the prototype chain as expected
Object.defineProperty(MySubClass.prototype, "constructor", {
enumerable: false, // this is merely a preference, but worth considering, it won't affect the inheritance aspect
value: MySubClass
});
// place extended/overridden methods here
MySubClass.prototype.superAwesomeMethod = function(){
alert("I'm super awesome!");
};
var testInstance = new MySubClass("hello");
alert(testInstance instanceof MyClass); // true
alert(testInstance instanceof MySubClass); // true
var MyClass=函数(条形){
this.foo=bar;
};
MyClass.prototype.awesomeMethod=函数(){
警惕(“我太棒了”)
};
//扩展MyClass
var MySubClass=函数(条){
MyClass.call(this,bar);//现在您可以使用ES6类的子类化:
类向量扩展数组{
sum(){
返回此.reduce((总计,值)=>总计+值)
}
}
设v2=新向量();
v2.推送(1);
v2.推送(2);
console.log(v2.sum());
console.log(v2.length);
v2.length=0;
console.log(v2.length);
console.log(v2);
比我先说,但是,是的,我宁愿创建一个对象,也不愿冒险弄乱其他对象与它的交互方式。我希望能够使用[]符号访问数组元素。这是老消息吗:?@shino:这不是老消息,不…@shino,你可以通过添加一个elementAt(I)来获得下一个最好的结果方法,然后让该方法实现使用[i]来获取element@shino,看到了吗?您计划实例化多少个向量数组?2?数百个?您忘记了一个变量。var Vector=[]
。看起来这对我来说最合适;谢谢。你得到了cookie。:)请记住,在数组上构建新的原型函数会将这些方法添加到所有创建的数组中。这可能不是你想要的,所以需要考虑。但是,这不允许你调用vector1[0]不幸的是,返回1.try console.log(v2.length)
var _Array = function _Array() {
if ( !( this instanceof _Array ) ) {
return new _Array();
};
};
_Array.prototype.push = function() {
var apContextBound = Array.prototype.push,
pushItAgainst = Function.prototype.apply.bind( apContextBound );
pushItAgainst( this, arguments );
};
_Array.prototype.pushPushItRealGood = function() {
var apContextBound = Array.prototype.push,
pushItAgainst = Function.prototype.apply.bind( apContextBound );
pushItAgainst( this, arguments );
};
_Array.prototype.typeof = (function() { return ( Object.prototype.toString.call( [] ) ); }());
var MyClass = function(bar){
this.foo = bar;
};
MyClass.prototype.awesomeMethod = function(){
alert("I'm awesome")
};
// extends MyClass
var MySubClass = function(bar){
MyClass.call(this, bar); // <- call super constructor
}
// which happens here
MySubClass.prototype = Object.create(MyClass.prototype); // prototype object with MyClass as its prototype
// allows us to still walk up the prototype chain as expected
Object.defineProperty(MySubClass.prototype, "constructor", {
enumerable: false, // this is merely a preference, but worth considering, it won't affect the inheritance aspect
value: MySubClass
});
// place extended/overridden methods here
MySubClass.prototype.superAwesomeMethod = function(){
alert("I'm super awesome!");
};
var testInstance = new MySubClass("hello");
alert(testInstance instanceof MyClass); // true
alert(testInstance instanceof MySubClass); // true
var extend = function(child, parent, optionalArgs){ //...
if(parent.toString() === "function "+parent.name+"() { [native code] }"){
optionalArgs = [parent].concat(Array.prototype.slice.call(arguments, 2));
child.prototype = Object.create(new parent.bind.apply(null, optionalArgs));
}else{
child.prototype = Object.create(parent.prototype);
}
Object.defineProperties(child.prototype, {
constructor: {enumerable: false, value: child},
_super_: {enumerable: false, value: parent} // merely for convenience (for future use), its not used here because our prototype is already constructed!
});
};
var Vector = (function(){
// we can extend Vector prototype here because functions are hoisted
// so it keeps the extend declaration close to the class declaration
// where we would expect to see it
extend(Vector, Array);
function Vector(){
// from here on out we are an instance of Array as well as an instance of Vector
// not needed here
// this._super_.call(this, arguments); // applies parent constructor (in this case Array, but we already did it during prototyping, so use this when extending your own classes)
// construct a Vector as needed from arguments
this.push.apply(this, arguments);
}
// just in case the prototype description warrants a closure
(function(){
var _Vector = this;
_Vector.sum = function sum(){
var i=0, s=0.0, l=this.length;
while(i<l){
s = s + this[i++];
}
return s;
};
}).call(Vector.prototype);
return Vector;
})();
var a = new Vector(1,2,3); // 1,2,3
var b = new Vector(4,5,6,7); // 4,5,6,7
alert(a instanceof Array && a instanceof Vector); // true
alert(a === b); // false
alert(a.length); // 3
alert(b.length); // 4
alert(a.sum()); // 6
alert(b.sum()); // 22
function SubArray(arrayToInitWith){
Array.call(this);
var subArrayInstance = this;
subArrayInstance.length = arrayToInitWith.length;
arrayToInitWith.forEach(function(e, i){
subArrayInstance[i] = e;
});
}
SubArray.prototype = Object.create(Array.prototype);
SubArray.prototype.specialMethod = function(){alert("baz");};
var subclassedArray = new SubArray(["Some", "old", "values"]);