Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Shorthand method definition fails to serialize and eval property

未关闭
#24 1 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
42/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
javascript
领域
tooling

调研方向

使用报告中所示的 serialize-javascript 调用重现该问题,重点关注 fn3 简写方法的输出。将其序列化形式与 fn1 和 fn2 进行比较,然后对结果求值,以确认语法错误已经消失。当简写方法被序列化为有效且可求值的 JavaScript,同时保留方法名时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

*properly

When using shorthand to define a function as a method in an object literal, serialization assigns a named function expression, which fails to evaluate.

$ var testSer = {  fn1: function(a) { console.log(a); return this; }, fn2: (d) => { console.log(d); return this; }, fn3(r) { console.log(r); return this; } };

Validate the functions first:


 test 1
{ fn1: [Function], fn2: [Function], fn3: [Function: fn3] }

$ testSer.fn2('test 2')

test 2
<context>

$ testSer.fn3('test 3')

test 3
{ fn1: [Function], fn2: [Function], fn3: [Function: fn3] }

Do the serialization:


$ ca
'{"fn1":function (a) { console.log(a); return this; },"fn2":(d) => { console.log(d); return this; },"fn3":fn3(r) { console.log(r); return this;  }}'

Note how it has created a "fn3" entry as a named function, but has neglected to add the function keyword.

Now try to eval:

$ var c = eval('('+ca+')');

SyntaxError: Unexpected token {
  at repl:1:20
  at REPLServer.defaultEval (repl.js:262:27)
  at bound (domain.js:287:14)
  at REPLServer.runBound [as eval] (domain.js:300:12)
  at REPLServer.<anonymous> (repl.js:431:12)
  at emitOne (events.js:82:20)
  at REPLServer.emit (events.js:169:7)
  at REPLServer.Interface._onLine (readline.js:211:10)
  at REPLServer.Interface._line (readline.js:550:8)
  at REPLServer.Interface._ttyWrite (readline.js:827:14)
  at ReadStream.onkeypress (readline.js:106:10)
  at emitTwo (events.js:87:13)
  at ReadStream.emit (events.js:172:7)
  at emitKeys (readline.js:1251:14)
  at next (native)
  at ReadStream.onData (readline.js:919:36)
  at emitOne (events.js:77:13)
  at ReadStream.emit (events.js:169:7)
  at readableAddChunk (_stream_readable.js:153:18)
  at ReadStream.Readable.push (_stream_readable.js:111:10)
  at TTY.onread (net.js:531:20)

All it needs to do is add the function keyword:

'{"fn1":function (a) { console.log(a); return this; },"fn2":(d) => { console.log(d); return this; },"fn3": function fn3(r) { console.log(r); return this;  }}'

Node version:

$ node -v
  v4.4.3
主要语言
JavaScript
星标
2.9k
派生
215
平均合并
1 天 12 小时
30 天内合并 PR
2

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

yahoo/serialize-javascript 的其他 Issue

查看 yahoo/serialize-javascript 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。