Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #28841

Re: Overwriting existing fucntion

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Overwriting existing fucntion
Date 2015-10-19 05:04 +0200
Organization albasani.net
Message-ID <n01mji$k3l$1@news.albasani.net> (permalink)
References <CbCdnUFUDeXC3rnLnZ2dnUU7-amdnZ2d@westnet.com.au>

Show all headers | View raw


On 2015-10-19 03:12, Andrew Poulos wrote:
> I need to "overwrite" one of the functions that the product produces 
> with one of my own. Custom code seems to always get added before their 
> code eg.
...
> function ap() {	
>    function action70(){
>      // my customised function
>    }
> }
> if (window.addEventListener) {
> 	window.addEventListener("load", ap);
> }

This doesn't work because action70() in this example is not visible
outside of ap(); it cannot overwrite a global function. Depending on
what exactly the fixed application code does, and which user agents have
to be supported, you could try using `Object.defineProperty()`:

    ////// your code /////////////

    function my_action70 () {
        console.log("custom function");
    }

    Object.defineProperty(window, "action70", { value: my_action70 });

    ////// fixed app code /////////

    function action70 () {
        console.log("original function");
    }

    ////// test ///////////////////

    action70();   // "custom function"


- stefan


PS, just saw your reply - the reason why your second version works is
because it creates a property on the global object - i.e., action70() is
now visible outside of ap().

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Overwriting existing fucntion Andrew Poulos <ap_prog@hotmail.com> - 2015-10-19 12:12 +1100
  Re: Overwriting existing fucntion Andrew Poulos <ap_prog@hotmail.com> - 2015-10-19 13:57 +1100
  Re: Overwriting existing fucntion Stefan Weiss <krewecherl@gmail.com> - 2015-10-19 05:04 +0200
    Re: Overwriting existing fucntion Stefan Weiss <krewecherl@gmail.com> - 2015-10-19 13:14 +0200
  Re: Overwriting existing fucntion Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-19 19:49 +0200
    Re: Overwriting existing fucntion Andrew Poulos <ap_prog@hotmail.com> - 2015-10-20 10:18 +1100
      Re: Overwriting existing fucntion Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-20 02:17 +0200
    Re: Overwriting existing fucntion Stefan Weiss <krewecherl@gmail.com> - 2015-10-21 19:47 +0200
      Re: Overwriting existing fucntion Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-21 21:40 +0200
        Re: Overwriting existing fucntion Stefan Weiss <krewecherl@gmail.com> - 2015-10-22 03:04 +0200
  Re: Overwriting existing fucntion Danny <dann90038@gmail.com> - 2015-10-19 13:29 -0700

csiph-web