I am new to JS and ran into an issue. Basically what I am trying to do is create a new .js which only contains variables lets called the file 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
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.
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.
You are using "new" on a JSON Object, when new should be used on a Class to create an instance.
you would use your code like this: import news from './users.js'; news. newuser
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.