+ 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?
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();
+ 4
Rebeka thanks.
+ 1
maybe with a boolean variable that changes to true once the ball is thrown