gpt4 book ai didi

javascript - 变量未设置返回值

转载 作者:行者123 更新时间:2023-11-28 19:33:12 25 4
gpt4 key购买 nike

class Person
constructor: (@name, @org, @phone, @email) ->
alert "#{name}: #{org} #{phone} #{email}"

__create = (title) ->
p =
name: ""
org: "_organization"
phone: "_phone"
email: "_email"

(p[pr] = "%#{title}#{p[pr]}%" for pr of p)

Person(p.name, p.org, p.phone, p.email)

m = __create "author"

alert m.name

这始终给我:

TypeError: Cannot read property 'name' of undefined

但是,编译后的 JavaScript 明确为 m 分配了一个值:

__create = function(title) {
var p, pr;
p = {
name: "",
org: "_organization",
phone: "_phone",
email: "_email"
};
for (pr in p) {
p[pr] = "%" + title + p[pr] + "%";
}
return Person(p.name, p.org, p.phone, p.email);
};

m = __create("author");

alert(m.name);

我在这里缺少什么?

最佳答案

您需要使用new运算符,例如,

__create = (title) ->
p =
name: ""
org: "_organization"
phone: "_phone"
email: "_email"

(p[pr] = "%#{title}#{p[pr]}%" for pr of p)

new Person(p.name, p.org, p.phone, p.email)

http://coffeescript.org/#classes

关于javascript - 变量未设置返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26367290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com