+ 2

Can I add methods to an object which is already declared in Javascript ??? If yes then how??

28th May 2017, 6:52 PM
Ahmed Imtiaz Ferdous
Ahmed Imtiaz Ferdous - avatar
2 Answers
+ 7
like this? var obj={}; obj.foo=function(){return 42;} alert(obj.foo()) or something that determined on runtime? var obj={}; if(prompt()==="1") obj.foo=function(){return 42;} else obj.foo=function(){return 24;} alert(obj.foo())
28th May 2017, 6:53 PM
Burey
Burey - avatar
0
I'm sorry if the question is stupid. I actually wanted to create a code to speed up or down the text in marquee tag. Can you tell me what's the problem here in the code below. <body> <button id=fast onclick=faster()>Faster</button> <button id=slow onclick=slower()>Slower</button></br> <marquee scrollamount=3>Are you ready!</marquee> <script> var f = document.getElementById("fast"); var s = document.getElementById("slow"); var m = document.getElementsByTagName("marquee"); m[0].fast= function(){return ++this.scrollamount}; m[0].slow= function(){return ++this.scrollamount}; f.onclick=function faster() { m[0].fast; } s.onclick=function slower() { m[0].slow; } </script> </body>
28th May 2017, 7:14 PM
Ahmed Imtiaz Ferdous
Ahmed Imtiaz Ferdous - avatar