1

I want to change all links assigned to window.location. So I assume location has getter & setter . That's why i cloned it & then i override the real window.location :

  var clonedLocation=Object.assign({},window.location);


            Object.defineProperty( window, 'location', {
              get: function (f) {
                return clonedLocation;
               },
              set:function(o){
                if(typeof o==='string'){
                  o=o+'?id=1';
                }
                clonedLocation=o;
              }
            } );

        };

The expected behavior (if overriding done ) is , when you write :

window.location='/go';

The script should redirect you to /go?id=1 NOT /go.

However the actual behavior is that this script redirects to /go

==> Thus, the window.location setter wasn't overridden ,

How to override the setter of window.location?

Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
  • 1
    See http://stackoverflow.com/questions/2073086/javascript-how-to-intercept-window-location-change – guest271314 Oct 01 '16 at 19:48
  • Regarding the former `AOP` tag, wrapping and reassigning already declared functionality (be it functions or methods) misses any aspect of _AOP_. Any language which wants to qualify for the latter has to provide abstraction levels for at least `Joinpoint`, `Advice` and `Aspect`. The use case described by the OP should be referred to as method modification, and JavaScript of cause is well suited for this scenario and could easily provide a complete `target`/`context` aware toolset of method modifiers like `around`, `before`, `after`, `afterThrowing` and `afterFinally` via `Function.prototype`. – Peter Seliger Sep 14 '22 at 17:14

2 Answers2

2

Javascript properties can be defined to have configurable: false indicating that they can not be redefined.

Attempt to redefine such property will result in an error like:

Uncaught TypeError: Cannot redefine property: location

It is to be expected that properties on host objects are not configurable for security reasons.

You can verify this by checking the property descriptor:

Property descriptor for location

lorefnon
  • 12,875
  • 6
  • 61
  • 93
  • Actually, being a *host object* property it doesn't even need its `configurable` attribute to be set to `false` for acting unpredictable. – Bergi Oct 01 '16 at 21:27
  • Yeah - very true. Not really relevant to this specific question, but having a generic way to detect if or not a property is configurable (which host objects also adhere to) helps consumer code use reflection to detect configurability instead of having to special case all the host objects. – lorefnon Oct 01 '16 at 21:30
0

you're used to wrap window.location with another static class which could implement hooks (before and after):

take a look into kaop

Aspects.add(
  function op1(){
    console.log("operation one");
    var argument0 = meta.args[0];
    if(typeof argument0==='string'){
        argument0=argument0+'?id=1';
    }
  },
  function op2(){
    console.log("operation two");
  }
)

var DummyLocation = Class({
  set: ["op1", function(newUrl){
    location = newUrl;
  }, "op2"],
  get: [function(){
    return location;
  }]
})

Now you should use:

DummyLocation.set("/go");
k1r0s
  • 359
  • 3
  • 14