+ 7

In javaScript objective programming...

How can we make such that This code : obj("ball").throw() ; returns Ball is thrown How can be defined this?

22nd Aug 2018, 2:09 AM
Googel
Googel - avatar
3 odpowiedzi
+ 2
function obj(arg){ if(arg==="ball") return{ throw: function(){ console.log("Ball is thrown"); } } else{ return; } } // Now this will return what you expect obj("ball").throw(); But that isn't object orientated programming!!! Better is doing this: function Ball(){ this.throw=function(){ console.log("Ball is thrown“); } } var ball= new Ball(); ball.throw();
24th Aug 2018, 2:16 PM
BraveHornet
BraveHornet - avatar
+ 4
Rebeka thanks.
8th Sep 2018, 9:58 AM
Googel
Googel - avatar
+ 1
maybe with a boolean variable that changes to true once the ball is thrown
22nd Aug 2018, 9:23 AM
Ultron
Ultron - avatar