1

I'm new to JavaScript.

I'm currently working on something that requires the following:

In JavaScript if my code structure looks as below and I may only write in code_1, is it possible to alter the way get and set methods are used to assign a property to an object, z, without changing a function, foo(z), in not_my_code? I can decide z in code_1, but I want to change the way its properties are assigned in foo(z).

I've read about using Object.prototype in some way, but since I'm very new to JavaScript this was my initial approach which probably is completely wrong since it doesn't work!

<script>
\\ code_1
var z;
Object.prototype = {
    // change the Object prototype behaviour when z is get or set in foo
    get: function() {
        // do something when z is used in foo
        // like output a console.log message
    }
}

</script>

<script>
\\ not_my_code
var bar = ( function() {
    foo : function(z) {// z is used}
})();

</script>
Alexander
  • 410
  • 5
  • 14
  • Does foo returns the `z` object? Can you alter it after it returns? – kbariotis Mar 05 '17 at 22:24
  • No, it's not returned, the returned object is something else, which is generated based on z. – Alexander Mar 05 '17 at 22:28
  • Possible duplicate of [How to detect when a JavaScript object variable is accessed?](http://stackoverflow.com/questions/41405080/how-to-detect-when-a-javascript-object-variable-is-accessed/); see also [Is JavaScript Proxy supposed to intercept direct changes to underlying object like Object.Observe?](http://stackoverflow.com/questions/40011480/is-javascript-proxy-supposed-to-intercept-direct-changes-to-underlying-object-li/) – guest271314 Mar 05 '17 at 22:29

0 Answers0