- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个脚本,该脚本从表的第一列中获取方法(以字符串的形式),并在每行的第三列中显示该方法的输出。前任。 html 表的第 1 行第 1 列包含 .getFullYear(),因此在第 3 列中我想运行(我的 Date 对象)now.getFullYear()。在下一行中,我将运行 now.getMonth() 等。
当我运行 eval("now."+method) 时,它运行没有问题,但我试图避免使用 eval()。当我运行 now[method] 时,输出返回未定义。为什么 eval() 可以工作,但现在 [method] 返回不同的东西?
```JavaScript`
const now = new Date();
//gets the full table data
const methodTable = document.querySelector("#methods-table");
function tableTimeData(table) {
const tableLength = table.rows.length;
for (let i = 1; i < tableLength; i++) { //iterate through each table row, skipping the header
let row = table.rows[i];
let method = row.cells[0].innerText;
row.cells[2].innerText = eval("now." + method);
//OR
method = method.substr(1);
row.cells[2].innerText = now[method];
}
}
tableTimeData(methodTable);
HTML
<table class="table" id="methods-table">
<tr>
<th>Method Name</th>
<th>Method Description</th>
<th>Method Output</th>
</tr>
<tr>
<td class="text-info font-weight-bold">.getFullYear()</td>
<td>Returns the full 4 digit year.</td>
<td></td>
</tr>
<tr>
<td class="text-info font-weight-bold">.getMonth()</td>
<td>Returns the month as an integer (0-11).</td>
<td></td>
</tr>
<tr>
<td class="text-info font-weight-bold">.getDay()</td>
<td>Returns the day of the week as an integer (0-6).</td>
<td></td>
</tr>
<tr>
<td class="text-info font-weight-bold">.getHours()</td>
<td>Returns the hour as of creation.</td>
<td></td>
</tr>
<tr>
<td class="text-info font-weight-bold">.getMinutes()</td>
<td>Returns minute as of creation</td>
<td></td>
</tr>
<tr>
<td class="text-info font-weight-bold">.getSeconds()</td>
<td>Returns second as of creation</td>
<td></td>
</tr>
<tr>
<td class="text-info font-weight-bold">.getTime()</td>
<td>Returns the number of miliseconds passed since Jan. 1 1970. Used as a baseline to compare 2 dates.</td>
<td></td>
</tr>
<tr>
<td class="text-info font-weight-bold">.toDateString()</td>
<td>Returns a shortened date with day, month, date and year with day and month as names rather than integers.</td>
<td></td>
</tr>
<tr>
<td class="text-info font-weight-bold">.toTimeString()</td>
<td>Returns a shortened time of day with hour, minute second and time zone (always Greenwhich Mean Time as a standard).</td>
<td></td>
</tr>
<tr>
<td class="text-info font-weight-bold">.toLocaleString()</td>
<td>Returns a shortened time of day with hour, minute second and time zone (as your local time zone).</td>
<td></td>
</tr>
</table>
我希望 eval("now."+method) 的结果与 now[method] 相同。
最佳答案
如果方法
是.getFullYear()
,那么
method = method.slice(1, -2);
row.cells[2].innerText = now[method]();
应该可以工作。您忘记从 method
中删除 ()
关于javascript - 为什么 object.eval() 有效,方括号返回未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57683152/
在我看来 eval()受到与 goto 相同的蔑视。由 eval ,我的意思是将字符串作为代码执行的函数,如在 PHP、Python、JavaScript 等中看到的。是否有使用 eval() 的情况
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我有几十个条件(例如,foo > bar)需要在 DataFrame 的 ~1 MM 行上进行评估,最简洁的方法编写此代码是为了将这些条件存储为字符串列表,并创建 bool 结果的 DataFrame
我正在创建一种编译为字节码并在自定义 VM 上运行的小型语言,其架构在很大程度上受到了我所阅读的有关 Python 和 Lua 的影响。有两个堆栈 - 一个存储函数参数、局部变量和临时值的数据堆栈,以
我正在尝试运行此代码: var aaa = await page.$$eval(selector, list => (list, value) => { return reso
我知道标题听起来很复杂,但为了保持动态,这是有目的的;) 示例(请注意,假定这些示例代码位于外部 eval 内) //Ex1 this works eval('function test (){}')
这是交易,我们有一个很大的 JS 库需要压缩,但是 YUI compressor如果发现“eval”语句,它不会完全压缩代码,因为担心它会破坏其他东西。这很好,但是我们确切地知道什么正在被评估,所以我
每门计算机科学类(class)中都讲授过这一点,并且在许多书籍中都写到程序员不应使用 GoTo。甚至还有一个 xkcd关于它的漫画。我的问题是我们是否达到了可以对 Eval 说同样的事情的地步? Go
这两种方法中的一种是否必须被优先使用,还是仅取决于口味? #!/usr/bin/env perl use warnings; use strict; use DBI; my $db = 'sqlite
如果存在“eval()”,uglify 不会破坏变量。命令行: uglifyjs script/script.js --compress --mangle --unsafe/path/to/scrip
我刚刚开始使用 Julia。我正在尝试使用 eval(在 Julia 中)来在函数中定义一组变量。假设我想将 v1 设置为 2: function fun_test(varargs...) v1
这个问题在这里已经有了答案: When is JavaScript's eval() not evil? (27 个答案) 关闭2 年前。 我从未遇到过需要 eval() 的情况。 人们常常说 []
这个问题在这里已经有了答案: "Variable" variables in JavaScript (9 个回答) Use dynamic variable names in JavaScript
好的 - 我有一个非常具体的案例,我需要使用 eval()。在人们告诉我我根本不应该使用 eval() 之前,让我透露一下我知道 eval 的性能问题、安全问题和所有这些问题。我在非常狭窄的情况下使用
我的问题是关于 JavaScript 闭包和 Eval() 函数。 我有一些看起来像这样的代码,还有一些其他 jQuery 插件相关的代码没有显示。如果需要,我可以用更多代码更新问题。 var _Cu
...或者有更好的方法来实现记忆化吗? Function.memoize = function(callableAsString) { var r = false, callable,
当我尝试在jmeter中执行以下代码时: import org.json.JSONArray; import org.json.JSONObject; String jsonString = prev
根据Mozilla docs为了使用 eval 执行函数,它必须被包装在 ( ) 中,即如果您不使用它们,那么它将被视为字符串. eval as a string defining function
标题看起来很蠢,但我不知道如何准确表达,抱歉。 我有一个程序需要评估一些用户代码(通过 RestrictedPython 以确保安全),并且我想将一个函数放入评估的全局变量中,以便它可以在评估时向我打
以下需要在函数范围内,因为在交互式控制台模式下不会发生奇怪的行为。 以下函数按预期返回 5 (function() { var x = 5; return eval("x"); })() 一个简单透明
我是一名优秀的程序员,十分优秀!