gpt4 book ai didi

javascript - 函数不读取对象

转载 作者:行者123 更新时间:2023-11-28 16:17:49 25 4
gpt4 key购买 nike

有人可以告诉我如何让发送函数从下一个代码中读取电子邮件对象吗?

var email = {
to: 'google@gmail.com',
subject: 'new email',
text: 'helloWorld'
}

function send() {
var sendMe = new email();
console.log(sendMe.subject);

}
send();​

我收到此错误,我还尝试按如下方式声明电子邮件:

var email = new object(); 

但没有成功

Uncaught TypeError: object is not a function 

最佳答案

您要么尝试执行此操作:

var email = { to: 'google@gmail.com', subject: 'new email', text: 'helloWorld' }

function send()
{
console.log(email.subject);
}

send();

或者这个

function email()
{
this.to = 'google@gmail.com';
this.subject = 'new email';
this.text = 'helloworld';
}

function send()
{
var sendMe = new email();
console.log(sendMe.subject);
}

send();

我不确定是哪一个,所以我举了两个例子。干杯

关于javascript - 函数不读取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10866862/

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