<!doctype >
<html>
<meta utf="utf-8">
<header>
<title>PRACTISE</title>
<script >
function MakeMe(name , age , likes, shout){ //creates objects
return {
name : name,
age : age,
likes : likes,
shout : function(){
alert(shout + " " +last(this.likes));
}
}
}
function last(obj){ //gets last element of the array
var el=obj.pop();
obj.push(el);
return el;
}
var dammy = MakeMe("Damilola" , 20, ["Jesus" , "Programming"], "Ewooooooo");//creates an object
dammy.shout();
var dara = MakeMe("Daramola" , 17, ["God" , "Gaming" , "Ed sheeran"] , "Leaveeeeeeeeeeee");//creates an object
// i wrote the following codes below to check if dammy and dara are really different objects
alert(last(dara.likes));
dara.shout();
dammy.likes.push("Taylor");
alert(last(dara.likes));
alert(last(dammy.likes));
dammy.shout();
</script>
</head>
</html>
My main question is : Is this an ok way of creating objects in javascript as oppose to the normal method of using a constructor and using the "new" keyword. If there is any side effect of creating objects using the above method. Thank You
//Normal way is
function MakeMe(name ,age, likes, shout){
this.name = name;
this.age= age;
this.likes={"me", "you", "us"};
this.shout = function(){
alert(shout + " " +last(this.likes));
};
}
var dammy = new MakeMe("D" , 17, ["God" , "Gaming" , "Lorde"] , "Loveee");