首页 \ 问答 \ 简单的Javascript代码中的奇怪问题,非空数组参数到达函数为空(Strange issue in simple Javascript code, non-empty array parameter reaches function empty)

简单的Javascript代码中的奇怪问题,非空数组参数到达函数为空(Strange issue in simple Javascript code, non-empty array parameter reaches function empty)

我最近一直在练习JS / ECMAScript我在网上找到的例子,在尝试一个关于反转数组的例子时,我遇到了一个非常奇怪的问题。 我下面的代码是完整的,但是半工作。

"use strict";

function reverseArray(theArray){
  let tempArray = [];

  if(theArray && theArray.length > 0){
    console.log('Here works!');
    while(theArray.length > 0){
      tempArray.unshift(theArray.shift());
    }
  }

  return tempArray;
}

function reverseArrayInPlace(theArray){
  console.log(theArray);
  if(theArray && theArray.length > 0){
    console.log('Should go through here!');
    for(let i = 0; i < Math.floor(theArray.length/2); i++){
      let temp = theArray[i];
      theArray[i] = theArray[theArray.length - 1 - i];
      theArray[theArray.length -1 - i] = temp;
    }
  }

  return theArray;
}

let firstSacrifice = ['I','n','v','e','r','t','M','E'];
let secondSacrifice = ['I','n','v','e','r','t','M','E','A','g','a','i','N'];

console.log(firstSacrifice);
console.log('InvertME >>', reverseArray(firstSacrifice));
console.log('InvertME >>', reverseArrayInPlace(firstSacrifice));
console.log('\n');
console.log(secondSacrifice);
console.log('InvertMEAgaiN >>', reverseArray(secondSacrifice));
console.log('InvertMEAgaiN >>', reverseArrayInPlace(secondSacrifice));

第一个函数reverseArray按预期工作:它将给定的数组复制到另一个数组中。 问题在于第二个函数reverseArrayInPlace 。 从理论上讲,它应该在它收到的同一个给定数组中进行反转,但奇怪的是,参数theArray到达函数empty [] 。 因此,根本没有完成该功能的逆转。

我正在使用node (v6.10)在shell上测试代码,但在这种情况下,我也在firefoxchromeedge的控制台中执行它,结果非常相似。 由于某种原因,我无法理解(和往常一样,这种愚蠢的错误,我已经尝试了几个小时),参数theArray在到达时是空的,当然,根本没有给出任何错误或警告。

顺便说一下,我在本教程中找到了关于Javascript的练习。 我知道它有点老了(是的,我已经阅读过关于MDN上JS的文档)但是为了实践起见,提出的练习很有趣。

哦, 如果你想摆弄这段代码请点击这里 ,但记得打开控制台查看日志条目!

谢谢!


I've been practising JS/ECMAScript lately with examples that I find on the net and while trying one about reversing arrays, I've hit a really strange problem. The code that I put below is complete but half-working.

"use strict";

function reverseArray(theArray){
  let tempArray = [];

  if(theArray && theArray.length > 0){
    console.log('Here works!');
    while(theArray.length > 0){
      tempArray.unshift(theArray.shift());
    }
  }

  return tempArray;
}

function reverseArrayInPlace(theArray){
  console.log(theArray);
  if(theArray && theArray.length > 0){
    console.log('Should go through here!');
    for(let i = 0; i < Math.floor(theArray.length/2); i++){
      let temp = theArray[i];
      theArray[i] = theArray[theArray.length - 1 - i];
      theArray[theArray.length -1 - i] = temp;
    }
  }

  return theArray;
}

let firstSacrifice = ['I','n','v','e','r','t','M','E'];
let secondSacrifice = ['I','n','v','e','r','t','M','E','A','g','a','i','N'];

console.log(firstSacrifice);
console.log('InvertME >>', reverseArray(firstSacrifice));
console.log('InvertME >>', reverseArrayInPlace(firstSacrifice));
console.log('\n');
console.log(secondSacrifice);
console.log('InvertMEAgaiN >>', reverseArray(secondSacrifice));
console.log('InvertMEAgaiN >>', reverseArrayInPlace(secondSacrifice));

The first function, reverseArray, works as expected: it copies the given array into another. The problem comes with the second function, reverseArrayInPlace. In theory, it should do the reversal in the same given array it receives but, strangely, the parameter theArray reaches that function empty []. Therefore, no reversal done with that function at all.

I'm testing the code on a shell with node (v6.10), but in this case I also executed it in the consoles of firefox, chrome and edge with the very same result. For some reason that I cannot fathom (and as usual with this kind of silly errors, I've been trying for a good few hours), the parameter theArray is empty on arrival and, of course, no errors or warnings are given at all.

By the way, I found the exercise proposed in this tutorial about Javascript. I know it's a bit old (and yes, I've already read the docs about JS on MDN) but the exercises proposed there are interesting enough for practice's sake.

Oh, and click here if you want to fiddle with this code, but remember to open the console to see the log entries!

Thanks!


原文:https://stackoverflow.com/questions/42847142
更新时间:2024-01-19 06:01

最满意答案

public abstract class AbstractFoo<T>  {

    public void process(T a) {

        //do common processing

        doProcess(a);
    }

    protected abstract void doProcess(T a);
}

public class Person extends AbstractFoo<Person> {
    @Override
    protected void doProcess(Person p) {
        p.draw();
    }
}


public class Car extends AbstractFoo<Car> {
    @Override
    protected void doProcess(Car c) {
        c.draw();
    }
}

public abstract class AbstractFoo<T>  {

    public void process(T a) {

        //do common processing

        doProcess(a);
    }

    protected abstract void doProcess(T a);
}

public class Person extends AbstractFoo<Person> {
    @Override
    protected void doProcess(Person p) {
        p.draw();
    }
}


public class Car extends AbstractFoo<Car> {
    @Override
    protected void doProcess(Car c) {
        c.draw();
    }
}

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)