我正在参加一个关于JavaScript中函数式编程的很酷的在线课程.我一直很顺利,直到指导员使用它Array.prototype.reject()并且在运行时它对我不起作用.
我想使用"reject"而不是for循环,因为它代码较少.但是,我的浏览器,NodeJS和Express Code控制台告诉我reject is not a function.
我研究过其他讨论的文章promise.reject不是函数,而是提供对我的场景没有意义的解决方案.
以下是课程中的示例代码:
var animals = [
{ name: 'Fluffykins', species: 'rabbit' },
{ name: 'Caro', species: 'dog' },
{ name: 'Hamilton', species: 'dog' },
{ name: 'Harold', species: 'fish' },
{ name: 'Ursula', species: 'cat' },
{ name: 'Jimmy', species: 'fish' }
];
var isDog = function(animal){
return animal.species === 'dog';
}
var otherAnimals = animals.reject(isDog);
Run Code Online (Sandbox Code Playgroud)
使用for循环解决方法:
var notDogs = animals.filter(function(animal){
return animal.species !== 'dog';
});
Run Code Online (Sandbox Code Playgroud)
它的输出是: …
我在 Windows 10 环境中使用 python 3.5.2 解释器。
\n\n我根据 Google 的 Python 课程输入了以下几行:
\n\n>>> import sys,os,codecs\n>>> f=codecs.open(\'foo.txt\',\'rU\',\'utf-8\')\n>>> for line in f:\n... f.write(\'\xc2\xa3 $\')\n...\nTraceback (most recent call last):\n File "<stdin>", line 2, in <module>\n File "C:\\Users\\rschostag\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\codecs.py", line 718, in write\n return self.writer.write(data)\n File "C:\\Users\\rschostag\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\codecs.py", line 377, in write\n self.stream.write(data)\nio.UnsupportedOperation: writ\nRun Code Online (Sandbox Code Playgroud)\n\nfoo.txt 的内容当前为:
\n\nstring1\nstring2\nRun Code Online (Sandbox Code Playgroud)\n\nfoo.txt,根据记事本中的“另存为...”,是 ANSI。是否需要将其转换为 UTF-8 才能将 UTF-8 字符写入文件?
\n我正在学习一门名为"Javascript Tutorial"的精彩JavaScript课程.视频说明显示了我没有发生的事情.应该发生的事情是单击带有HTML标记的图像logo使其消失,然后单击一个显示"获取徽标"的按钮将其恢复.
以下是jstut.html我在整个课程中构建的文件的摘录:
<!doctype html>
<html lan="en">
<head>
<meta charset="utf-8">
<script src="jstut.js"></script>
<style type="text/css">
body {font-size: 1.6em;}
.hidden {display:none;}
.show {display:inLine !important;}
button {
border: 2px solid black; background: #ESE4E2;
font-size: .5em; font-weight: bold; color: black;
padding: .8em 2em;
margin-top: .4em;
}
</style>
</head>
<body>
<img src="ntt-logo.png" id="logo">
<button id="logoButton" type='text'>Get Logo</button>
<script>
document.getElementById('logoButton').onClick = function(event){
document.getElementById('logo').className = "show";
}
document.getElementById('logo').onClick = function(event){
document.getElementById('logo').className = "hidden";
}
</script>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
我将我的语法与课程的语法进行了比较,这是完全匹配的.点击它后,请帮我把图像消失.
我正在使用Firefox.我尝试使用IE和Chrome,但它的行为方式相同.