0

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

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 
4

4 回答 4

4

You are either trying to do this:

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

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

send();

Or this

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();

I'm not sure which, so I made an example of both. Cheers

于 2012-06-03T00:03:31.213 回答
0

It sounds like you want sendMe to point at the same data email is holding:

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

But if this is the case, you might as well skip the extra variable and just use email directly:

var email = { ...} ;
function send() {
   console.log(email.subject);
}
于 2012-06-03T00:03:21.770 回答
0

You can't use an identifier as an object constructor unless it's a function.

If you want a reference to the object that you created, just copy it from the variable:

var sendMe = email;
于 2012-06-03T00:04:02.730 回答
0

You have to return object:

var email = function() {
    return {
        to: 'google@gmail.com',
        subject: 'new email',
        text: 'helloWorld'
    }
};

and then

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

should work.

于 2012-06-03T00:04:18.903 回答