- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有点不知所措,但我想知道是否有人可以帮助我解决这个问题:
取自:http://ejohn.org/apps/learn/#43
function highest(){
return arguments.slice(1).sort(function(a,b){
return b - a;
});
}
assert(highest(1, 1, 2, 3)[0] == 3, "Get the highest value.");
assert(highest(3, 1, 2, 3, 4, 5)[1] == 4, "Verify the results.");
我认为应该是:
Array.prototype.highest = function(){
return arguments.slice(1).sort(function(a,b){
return b - a;
});
}
assert(highest(1, 1, 2, 3)[0] == 1, "Get the highest value.");
assert(highest(3, 1, 2, 3, 4, 5)[1] == 1, "Verify the results.");
但这给我带来了未定义的错误。
最佳答案
您不是在数组上调用它。
assert([].highest(1, 1, 2, 3)[0] == 1, "Get the highest value.");
assert([].highest(3, 1, 2, 3, 4, 5)[1] == 1, "Verify the results.");
几乎可以工作([]
可以是任何数组)。但是,您仍然没有将 arguments
转换为数组,也没有使用 call
调用 slice
或 apply
.这是练习的重点。
此外,它没有任何意义,因为您没有使用数组的内容。
因此,解决方案是:
function highest(){
return Array.prototype.slice.call(arguments, 1).sort(function(a,b){
return b - a;
});
}
关于javascript - John Resig 高级 Javascript 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4301920/
我指的是 this article .我基本上是在创建一个带有 init 函数的类,该函数设置了一个 options 属性。见http://jsbin.com/usaboh/1/edit对于工作示例(
我指的是 this article . 在其中,他定义了一个看起来像这样的函数: function makeClass() { return function _class() {
我正在尝试引用使用 Resig 的“扩展”的现有代码,但我遇到了一堆错误 -------- 测试.ts -------- /// var m = new MyClass (3); --------
我正在尝试使用 John Resig 的继承类来扩展我正在开发的一些类。这些类具有被重写的方法。尽管如此,这些方法的原始版本也应该被调用。我的问题是,当我尝试调用父类(super class)上的方法
我理解基于参数对象或通过显式检查参数的类型和长度来实现 JavaScript 重载。请解释一下这篇文章:http://ejohn.org/blog/javascript-method-overload
我有一个看起来像这样的模板: " id="Edit.ID" name="Edit.ID" /> test
在他的书“Javascript Ninja 的 secret ”中,John Resig 写道: Although dynamic code evaluation has been maligned
以下代码摘自 Jon Resig 的书 Secrets of JavaScript Ninja解释如何使用闭包来实现函数的部分应用。但是,我在理解变量 arg 的意图时遇到了问题。为什么需要它以及它如
所以我的目标是使用John Resig's templating engine . 我需要向函数传递一个包含"template"的变量。然而,问题是我需要传递类似的东西: console.log("
Function.prototype.bind = function(){ var fn = this, args = Array.prototype.slice.call(argument
我正在寻找一种简单的方法来创建两个类,一个类继承另一个类,子类重新定义父类的方法之一,并在新方法中调用父类的方法。 例如,有一个类 Animal 和 Dog,其中 Animal 类定义了一个方法 ma
我正在使用 John Resig 的简单 javascript inheritance code建立和扩展类。我有一些像他的例子这样的类(class): var Person = Class.exte
我已经实现了与 John Resig's Class library 兼容的简单 Mixin 行为.我要求其他 JS 人员检查代码并让我知道这是否是 Mixin 的正常工作方式(我以前没有使用过它们)
我想测试我的js代码的性能,我正在寻找perf John Resig 在他的书中的函数 "Secrets of Javascript Ninja" .有谁知道如何创建这样的函数或简单函数(与 John
我有点不知所措,但我想知道是否有人可以帮助我解决这个问题: 取自:http://ejohn.org/apps/learn/#43 function highest(){ return argume
在过去的一周里,我一直在努力学习关于 js 继承的知识,并遇到了 john resig 的简单 js 继承脚本。测试代码时一切正常,直到我尝试迭代我的扩展对象之一。这是代码: var Person =
我一直在我的项目中使用 John Resig 的简单继承方案。 http://ejohn.org/blog/simple-javascript-inheritance/ 基本上,我有一个想要用作数组的
我一直在试验 John Resig's micro-template ,效果很好。但是,标记不会通过 XHTML 1.0 Transitional 验证测试。 (除其他事项外,id 属性会产生错误。)
我在使用 John Resig 的 Micro 模板时遇到了一些麻烦。 谁能帮我解决为什么它不起作用? 这是模板 test content {%=id%} {%=name%} 以及引擎的修改部分
下面的示例代码是 John Resig 的 Learning Advnaced JavaScript 中的#36。 http://ejohn.org/apps/learn/#36 It is call
我是一名优秀的程序员,十分优秀!