I've been studying javascript lately.
I'm trying to modify an existing plug-in.
Can I modify the original sauce without fiddling with it?I'm looking for it.
I am wondering if I can insert my own action to overwrite the function.
Please let me know if there is any other good way.
Thank you for your cooperation.
HTML source.
<divid="hogehoge">
</div>
This is what javascript looks like.
<script>
(
function(arg)
{
arg(jQuery);
}
(
function($)
{
function Hoge(element){
this.element=element;
This.bazz();
}
Hoge.prototype.bazz=function(){
varmsg=this.foo();
This.element.html("<h1>"+msg+"</h1>");
}
Hoge.prototype.foo=function(){
varmsg="Hello Japan"
return msg;
}
$.fn.call_me=function(){
hoge = new Hoge(this);
return hoge;
}
}
)
);
var local_hoge=$("#hogehoge").call_me();
/*
I would like to add "and America!" to the msg of foo using local_hoge here.
False Code:
local_hoge.prototype.foo=function(){
varmsg = super // Intends to call the original code
msg+="and America!";
return msg;
}
*/
</script>
Rewrite itself is not a very good way, but you can rewrite the prototype
as follows:
(
function(arg){
arg(jQuery);
}
(
function($){
function Hoge(element){
this.element=element;
This.bazz();
}
Hoge.prototype.bazz=function(){
varmsg=this.foo();
This.element.html("<h1>"+msg+"</h1>");
}
Hoge.prototype.foo=function(){
varmsg="Hello Japan"
return msg;
}
$.fn.call_me=function(){
hoge = new Hoge(this);
return hoge;
}
}
)
);
var local_hoge=$("#hogehoge").call_me();
(function(local_hoge){
varproto=Object.getPrototypeOf(local_hoge);
var origin_func=proto.foo;
proto.foo=function(){
varmsg=orig_func();
msg+="and America!";
return msg;
};
})(local_hoge);
$("#hogehoge").call_me();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<divid="hogehoge">
</div>
© 2024 OneMinuteCode. All rights reserved.