简体   繁体   English

TypeError:_user2.default不是构造函数JS

[英]TypeError: _user2.default is not a constructor JS

I am new to JS and ran into an issue. 我是JS的新手,遇到了一个问题。 Basically what I am trying to do is create a new .js which only contains variables lets called the file users.js 基本上我想做的是创建一个新的.js,其中只包含名为“ users.js ”文件的变量

var newuser = {
firstname:'Tony',
lastname: 'franklyn',
email:    `franky@gmail.com`,
password: 'Franklin123',
};

var User = {
email:    `checker@gmail.com`,
password: 'Checks',
 };

module.exports = {
newuser
};

Now in another file called sign up 现在在另一个名为“ 注册”的文件中

  import news from './users.js';
  const newUser = new news();

 .typeText(Newuser.firstname)

I am trying to reference newuser But after I run my code I get a TypeError: _user2.default is not a constructor 我正在尝试引用newuser,但是运行代码后,我收到TypeError:_user2.default不是构造函数

Can someone explain why this is happening? 有人可以解释为什么会这样吗?

You cannot use the new operator on an object literal because the object literal is an object and not a class. 您不能在对象文字上使用new运算符,因为对象文字是对象而不是类。

You need to structure your code as follows: 您需要按以下方式构建代码:

// user.js
function User() {
    return {
        firstname: 'Tony',
        lastname: 'franklyn',
        email: `franky@gmail.com`,
        password: 'Franklin123'
    }
}


let user = new User();

You may also use the class keyword if you are using ES6. 如果使用的是ES6,也可以使用class关键字。

You are using "new" on a JSON Object, when new should be used on a Class to create an instance. 当在Class上使用new来创建实例时,您正在JSON对象上使用“ new”。

you would use your code like this: import news from './users.js'; 您将使用如下代码:从“ ./users.js”导入新闻; news. 新闻。 newuser 新用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM