简单的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上测试代码,但在这种情况下,我也在firefox , chrome和edge的控制台中执行它,结果非常相似。 由于某种原因,我无法理解(和往常一样,这种愚蠢的错误,我已经尝试了几个小时),参数
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 parametertheArray
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
最满意答案
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(); } }
相关问答
更多-
使用类型参数: package base { trait Base[T <: Base[T]] { def mix(other: T): T } trait BaseHandler[T <: Base[T]] { def defaultBase: T def handleBaseList(list: List[T]): List[T] = list.map(b => b.mix(defaultBase)) } } package someimp ...
-
CRTP从未得到实施的定义或有条件的支持,IIRC在发明时是令人惊喜的并且从此被接受。 从[班级] 在看到类名后, 类 名将被插入到它立即声明的范围中。 类名也被插入到类本身的范围中; 这被称为注入类名称 。 其中class-name是正在声明的类的名称。 因此, 类名在base-clause之前已经是可见的, base-clause是基础列表,但类只有在完整定义后才是完整的。 但是,允许模板使用不完整类型作为其类型参数,从[temp] 模板类型参数可能是不完整的类型。 请注意,即使其类型参数不是,模板也是 ...
-
Swift在实现协议方法时提供泛型方法类型参数(Swift supply generic method type parameter when implementing protocol method)[2023-05-15]
你的协议 protocol ContentService { func loadNextPage(pageStartId: T) -> [Datum] //Other methods... } 需要一个泛型方法loadNextPage ,和 func loadNextPage (pageStartId: Int) -> [Datum] { //Do something with pageStartId typed as an Int return p ... -
限制类型模板参数以仅采用特定模板的实例(Constrain type template parameter to only take instantiations of a specific template)[2022-06-11]
专业化就是答案。 templateclass C; template class Accepted { ... }; template class C > { C() = default; ... }; 以上使得任何C >都是格式良好的,因为它在实例化时选择了专门化。 虽然任何其他类型的C 选择未定义的主特殊化,所以不会编译。 Specialization is ... -
强制接口方法的参数是每个类实现的具体类型(Force parameter of interface method to be a concrete type per class implementation)[2022-03-16]
使用两个原型 我的建议是使用Run()两个方法原型,根据调用者的作用公开不同的原型。 通过这种方式,您可以提供Run()的类型安全和通用版本。 说明 问题是你有两个相互冲突的要求: 您希望能够对所有车辆进行相同处理,即将它们存储在List并且能够遍历它们并在每个List 调用Run()方法。 您希望每种车型仅允许某些类型的参数,即您需要针对每种车型的不同编译时规则。 因此,您实际上不希望能够遍历它们并在每个方面运行Run() ; 实际上你特别想要一个编译时错误。 这个声音非 ... -
与签名中需要实现类型参数的方法的接口(Interface with a method that requires parameter of implementation type in signature)[2023-06-30]
我觉得这样的东西就是你要找的东西: interface Equatable{ public boolean isEqual(T instance); } class C implements Equatable { boolean isEqual(C instance); } 这类似于标准的Comparable接口。 I think something like this is what you are looking for: interface Equatable ... -
您可以专门为该类型的方法。 例如 template
struct TemplatedClass { std::string methodA () {return "T methodA";} std::string methodB () {return "T methodB";} std::string methodC () {return "T methodC";} }; // Specialise methodA for int. template<> ... -
您可以使用泛型类型参数执行此操作。 trait A[T <: B] { def fun1( t:T ):C } class X1 extends A[B1]{ override def fun1(b:B1):C1 = ... } You can do this with a generic type parameter. trait A[T <: B] { def fun1( t:T ):C } class X1 extends A[B1]{ override def fu ...
-
解决问题的方法有三种,一种是is_specialization_of的实现,另一种是让函数使用std::basic_string
代替TString ,第三种方法与TString相同解; 使一个模板只能由std::basic_string匹配。 由于两个原因, is_base_of在您的示例中不够用: is_base_of用于查看类型U是否从T派生(或者它是否是相同类型),在您的代码段中不涉及继承。 std::basic_string不是一个完整的类型,因此根本不能和is_base_of一 ... -
public abstract class AbstractFoo
{ public void process(T a) { //do common processing doProcess(a); } protected abstract void doProcess(T a); } public class Person extends AbstractFoo { @Override protect ...