Javascript 函数不';我看不懂这个物体

Javascript 函数不';我看不懂这个物体,javascript,jquery,json,object,Javascript,Jquery,Json,Object,有人能告诉我如何让发送函数从下一个代码中读取电子邮件对象吗 var email = { to: 'google@gmail.com', subject: 'new email', text: 'helloWorld' } function send() { var sendMe = new email(); console.log(sendMe.subject); } send();​ 我收到此错误,我还尝试声明电子邮件如下: var email =

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

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 

听起来您想让
sendMe
指向
电子邮件中包含的相同数据:

var email = { ...} ;
function send() {
   var sendMe = email;
   console.log(sendMe.subject);
}
但如果是这种情况,您也可以跳过额外的变量,直接使用
电子邮件

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

听起来您想让
sendMe
指向
电子邮件中包含的相同数据:

var email = { ...} ;
function send() {
   var sendMe = email;
   console.log(sendMe.subject);
}
但如果是这种情况,您也可以跳过额外的变量,直接使用
电子邮件

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

您正在尝试这样做:

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

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

您正在尝试这样做:

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

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

除非标识符是函数,否则不能将其用作对象构造函数

如果要引用所创建的对象,只需从变量中复制它:

var sendMe = email;

除非标识符是函数,否则不能将其用作对象构造函数

如果要引用所创建的对象,只需从变量中复制它:

var sendMe = email;

您必须返回对象:

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

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

应该可以工作。

您必须返回对象:

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

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

应该有效。

这将有效,但在这种情况下,“new”关键字无效。不妨只写
var sendMe=email()因为它返回了一个新对象,有什么区别?在您的回答中,您使用
这个
,它基本上与
发送给我的对象相同。我只是展示了方法,它不是完美的代码:)是的,它将产生相同的对象,但在您的代码中,
new
关键字是不必要的,因为该对象是在函数内部创建的。使用
new
关键字基本上就是创建两个对象并返回其中一个。它可以工作,但不是最优的,这就是我所说的,我刚刚复制并粘贴了原始代码的那部分,所以
new
was incident;)这将起作用,但在这种情况下,“new”关键字是无用的。不妨只写
var sendMe=email()因为它返回了一个新对象,有什么区别?在您的回答中,您使用
这个
,它基本上与
发送给我的对象相同。我只是展示了方法,它不是完美的代码:)是的,它将产生相同的对象,但在您的代码中,
new
关键字是不必要的,因为该对象是在函数内部创建的。使用
new
关键字基本上就是创建两个对象并返回其中一个。它可以工作,但不是最优的,这就是我所说的,我刚刚复制并粘贴了原始代码的那部分,所以
new
was incident;)