0
How to execute object methods
So I need to create an object with it properties and methods, so that methods work based on properties. The idea of the following code is that if the users age is under 18 he/she may not gain access to the website So I need a help to make this code work. var person={gender1:"male",gender2:"female",age:prompt("Please enter your age"), func: function accessSite(person[gender1],person[gender2],person[age]){ if(gender1==="male" || gender2==="female"&& age>=18){ alert("Access is gained");} else if (gender1==="male" || gender2==="female"&& age<18) { alert("You are not allowed to access this website"); }accessSite(); }; console.log(person.func());
1 Réponse
+ 2
Take a look at this: https://www.w3schools.com/js/js_object_methods.asp
Basically you can do the exact same thing they did, just with an added if-statement:
var userAge = prompt("How old are you?");
function person(gender, age) {
this.gender = gender;
this.age = age;
}
var user = new person("male", userAge);
if(user.age =>18){
alert("You can access");
}
else{
alert("You can't access");