Javascript 模拟阵列对象
面向对象JavaScript书中的问题:Imagine Array()不存在,数组文字符号也不存在。创建一个名为MyArray()的构造函数,其行为尽可能接近Array()。 我认为这将是一个很好的挑战来测试我的技能。这是我想出来的,但它不起作用,而且非常不完整。。我有点困惑:Javascript 模拟阵列对象,javascript,Javascript,面向对象JavaScript书中的问题:Imagine Array()不存在,数组文字符号也不存在。创建一个名为MyArray()的构造函数,其行为尽可能接近Array()。 我认为这将是一个很好的挑战来测试我的技能。这是我想出来的,但它不起作用,而且非常不完整。。我有点困惑: function MyArray(){ // PRIVATE FIELDS ----------------------- var initialData = arguments; var storage; //
function MyArray(){
// PRIVATE FIELDS -----------------------
var initialData = arguments;
var storage;
// PRIVATE METHODS ----------------------
function refresh(){ //this doesn't work :(
for(var i = 0; i < storage.length; i++){
this[i] = storage[i]
}
};
function initialize(){
storage = initialData;
refresh();
}
function count(){
var result = 0;
for(var item in this){
//console.log(item, parseInt(item), typeof item);
if(typeof item == 'number'){
result++;
}
}
return result;
};
initialize();
// PUBLIC FIELDS -------------------------
this.length = count();
// PUBLIC METHODS ------------------------
//todo:
this.push = function(item){
refresh();
}
this.pop = function(){}
this.join = function(){}
this.toString = function(){}
}
var c = new MyArray(32,132,11);
console.log(c, c.length);
函数MyArray(){
//私人领域-----------------------
var initialData=参数;
var存储;
//私有方法----------------------
函数refresh(){//这不起作用:(
对于(变量i=0;i
这不是针对任何生产代码或任何项目。只是为了尝试学习更多JavaScript。有人可以尝试帮助我使用此代码吗
for(var item in this){
if(typeof item == 'number')
属性名称始终是字符串。您需要检查它是否是从0到MaxArrayLength的数字的字符串表示形式。例如,您可以这样做
for (var i=0; i<4294967296; i++)
if (i in this)
result = i;
for(var i=0;i问题是您可以使用arguments对象。它不是使用array()创建的数组,因此您不会违反本练习的规则。以下是您需要执行的操作:
this.length = 0;
for (var i in arguments) {
this[this.length] = arguments[i];
this.length++;
}
我忘了提到任何对象都是关联数组,因此在练习中应用关联数组并不是错误的,因为我们不使用array()对象本身
问题作者:在您的示例中,您使用:this[“i]=storage[i]等于this.i=storage[i]。尝试删除引号,并像this[i]=storage[i]那样使用它我目前正在翻阅这本书,我可能应该尝试一些更新的东西,但它还没有过时,校长们仍然很健全……我认为他们是
无论如何,我选择了一个稍微不同的解决方案,尽管我确实从op如何处理字符串中获得了灵感。我认为这个练习的挑战是不要再创建任何数组,否则…这是一个有点疯狂的挑战,特别是对于语言新手来说
var MyArray = function () {
var args = arguments;
var length = 0;
for each(var item in args) {
this[length++] = item;
}
this.toString = function () {
var result = args[0];
for (var i = 1; i < args.length; i++) {
result += ',' + args[i];
}
return result;
}
this.length = length;
this.push = function (push) {
var newLength = args.length++;
args[newLength] = push;
this[newLength] = push;
return ++length;
}
this.pop = function () {
delete args[--args.length];
delete this[args.length];
length--;
return args;
}
this.join = function(joiner){
if(typeof arguments[0] === "undefined"){
joiner = ',';
}
var result = args[0];
for (var i = 1; i < args.length; i++) {
result += joiner + args[i];
}
return result;
}
}
var a = new MyArray(1, 2, 3, 'test');
console.log(a.toString());
console.log(a[a.length - 1]);
console.log(a.push('boo'));
console.log(a.toString());
console.log(a.pop());
console.log(a.toString());
console.log(a.join(','));
a.join(' isn\'t ');
var MyArray=函数(){
var args=参数;
变量长度=0;
对于每个(参数中的变量项){
此[length++]=项;
}
this.toString=函数(){
var结果=args[0];
对于(变量i=1;i
我的解决方案是:
function MyArray() {
this.length = 0;
for(i = 0; i < arguments.length; i++) {
this[this.length] = arguments[i];
this.length++;
}
this.toString = function(joiner = ',') {
let str = this[0] ? this[0] : '';
for(i=1;i<this.length; i++) {
str+= joiner + this[i];
}
return str;
};
this.push = function(value) {
this[this.length++] = value;
return this.length;
};
this.pop = function() {
let value = this[this.length -1];
delete this[--this.length]
return value;
};
this.join = function(joiner) {
return this.toString(joiner);
}
}
函数MyArray(){
这个长度=0;
对于(i=0;i 对于(i=1;数组对象的关键是“长度”属性是神奇的。一旦和谐对象代理成为现实,像这样的事情就不会那么难了。所以你用数组来复制数组?即使作为一个练习,对我来说也没有多大意义。如果你想学习继承和高级主题,我建议你从@elclandrs开始,那本书的+1,谢谢。@elclandrs:这是一个练习来自面向对象的JavaScript书籍。我喜欢这个想法。很多年前我在C中做过类似的事情,但我不知道现在如何在JS中处理它(至少不是以一种有意义和有效的方式),而没有指针。有趣的问题,谢谢:)我正试图通过这个[1],这个[2],这个[3]进行过滤,它会是这样的“索引”,但采用属性格式..通过myrefresh()创建
函数,我怎样才能只过滤名为properties的数字?我已经解释了我的答案,这是你想要的吗?请注意,因为在循环中,它们可能会迭代内置属性,例如toString
。对于使用对象的一般情况,这是正确的,但在这种情况下,我使用参数object:when I通过迭代,我们只得到函数参数(参数)