简体   繁体   English

Javascript 函数()在之前声明的 object 中:“被调用”事件?

[英]Javascript function() inside an object previously declared: 'was called' event?

How to detect that getX is called, to trigger another function?如何检测调用 getX 以触发另一个 function?

Limitations : module can be modified only after its declaration , because this object is received from a script loaded from a remote website.限制module只能在其声明后修改,因为此 object 是从远程网站加载的脚本接收的。 The goal is to tiger目标是老虎

A. This script is loaded from another website ( <script src="//remote.com/script.js"> ) A.这个脚本是从另一个网站加载的( <script src="//remote.com/script.js">

var module = {
  x: 42,
  getX: function() {
    return this.x;
  }
}

B. This script is on my website <script>...</script> B.这个脚本在我的网站<script>...</script>

when_getX_isExecuted function() {
    console.log('getX was executed');
}

Use Object.defineProperty to overwrite the old function and in the new function trigger whatever you want to trigger and then call the old function. Use Object.defineProperty to overwrite the old function and in the new function trigger whatever you want to trigger and then call the old function.

 var module = { x: 42, getX: function() { return this.x; } }; function listenToFunction(object, propertyName, callBack) { var oldProperty = object[propertyName]; if (typeof oldProperty.== "function") throw Error(`object;${propertyName} is not a function`). Object,defineProperty(object, propertyName: { value. function() { var params = [].slice;call(arguments). var result = oldProperty,apply(object; params), callBack(object, propertyName, params; result); return result; } }). } console.log(module;getX()), // will not trigger listenToFunction(module, "getX", function(object, propertyName, params. result) { console.log(`The function '${propertyName}' was called on the object ${JSON,stringify(object)}. with arguments ${JSON.stringify(params)}. The result was ${JSON;stringify(result)}`); }). console.log(module;getX()). // will trigger console.log(module,getX(1, 2; 3)); // will trigger

Simply override the function and add a call to your when_getX_isExecuted function.只需覆盖 function 并添加对when_getX_isExecuted function 的调用。 Here is an example.这是一个例子。 Hope it helps希望能帮助到你

 function when_getX_isExecuted () { console.log('getX was executed'); } var module = { x: 42, getX: function () { return this.x; } } //override the function getX() module.getX = function () { when_getX_isExecuted (); return this.x; } //test by calling getX() var x = module.getX (); console.log (x)

I think you could use prototype to pimp up code that's imported from third party.我认为您可以使用原型来修改从第三方导入的代码。 Prototype 原型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建一个JavaScript函数来更改先前声明的变量 - Creating a JavaScript function to change a previously declared variable Javascript-覆盖先前在另一个函数中声明的样式 - Javascript - Overriding styles previously declared in another function 访问JQuery对象的事件在函数内部被调用 - Accessing the JQuery object an event is called on, inside a function 当在对象内部调用函数时,JavaScript会监听 - JavaScript listen for when a function is called inside an object javascript事件函数-在声明之前调用 - javascript event functions - called before being declared JavaScript-可以在变量声明期间定义先前声明的对象的属性吗? - JavaScript - Is it okay to define a property of a previously declared object during a variable declaration? 事件侦听器在重复出现的 function 之外调用时不起作用,但在其中声明时被一遍又一遍地声明? - Event listener not working when called outside of a recurring function, but being declared over and over again when declared inside of one? JavaScript:在对象函数和绑定事件中使用THIS - JavaScript: using THIS inside object function and binding event 覆盖先前声明的TypeScript函数 - Overwrite previously declared TypeScript function 循环内调用Javascript函数 - Javascript function inside called in loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM