Add code to run automatically with each function in Javascript [duplicate] - javascript

This question already has answers here:
Can I override the Javascript Function object to log all function calls?
(6 answers)
Closed 5 years ago.
For debugging purposes, I was wondering if there's some way in which I can create a piece of code to run whenever a javascript function is called - like adding something to Function.prototype or Function.constructor etc.

You could use a "decorator-like".
Something as simple as executing
var yourFunction = function { // do stuff };
var yourSecondFunction = function { // do stuff };
var myDecorator = function( callback )
{
// do your stuff
// ...
// then execute function
callback();
};
// usage
myDecorator( yourFunction );
myDecorator( yourSecondFunction );
I don't think overwriting the prototype of Function is a good idea.

Related

PhantomJS: call a user defined/custom function within phantomjs [duplicate]

This question already has an answer here:
Cannot pass module functions to Page
(1 answer)
Closed 6 years ago.
i'm getting a ReferenceError when i call a function i defined myself inside the page.evaluate() of Phantom; what is the proper way to do that ?
for example:
function mySweetFunction(item) {
// process item....
}
page.evaluate(function(){
var item= document.getElementsById('item');
mySweetFunction(item);
});
then i'll get the error:
ReferenceError: Can't find variable: mySweetFunction
What is the proper way to do this ?
mySweetFunction is quite big, and i would prefer to keep it out of page.evaluate(...) if possible.
If you want to use a function inside page.evaluate() you have to put it there first:
page.evaluate(function(){
function mySweetFunction(item) {
// process item....
}
var item = document.getElementsById('item');
mySweetFunction(item);
});

What's the difference between $scope.myScope = function () and function () in AngularJS [duplicate]

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 7 years ago.
I've recently gone into developing with AngularJS. It's confusing to me the different between these two:
$scope.myScope = function () {
var x = 'do something with variable here';
$scope.anotherScope = x;
};
and
function myFunction () {
var x = 'do something with variable here';
$scope.anotherScope = x;
}
They both seem to be able to do the same thing (I use them a lot inside controllers). Is there a best practice for when and where to use these two?
$scope.myScope = function () {};
This means your function is a property of the scope object. So you can use it in your controller , html page even in your app. it can be referenced in different modules in the same app. so you just call it in your html page using the function name directly either onclick or onchange , anyhow depending on your need.
the other definition can only be used in your controller and is not the scope of your app. however if you define your function using "this.myScope = function(){};" then you can call the function in your html by using your controller. like ng-click = "controllerName.myScope();"
the main difference is i nwhich scope the function belongs to and where all you can reference the function.
hope it helps !!!!
As mourycy already mentioned, you should use the form
$scope.myScopeFunction = function () {
...
};
only for functions which you want to call via the scope object. This is needed for function calls within your views.
For example:
<button ng-click="myScopeFunction()" />
which calls the function myScopeFunction of the current $scope object.
If you don't need to be able to call a controller method from "outside" you should use the following form:
function myFunction() {
...
};
$scope.myScop = function(){
...
};
is a function you can execute from the HTML controller.
function foo(){
...
}
is a function you can only execute on the controller's JS file.

javascript scope in requestAnimationFrame [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 8 years ago.
I want to make a select option for animations, so whatever type is selected, it's shown on the canvas.
So i make each animation as a "class" :
(function (exports) {
function animationA() {}
animationA.prototype.init = function(){}
animationA.prototype.draw = function(){}
exports.animationA = animationA;
})(this);
Then in the main js:
var a = new animationA();
function setup() {
a.init();
}
function update(callback) {
requestAnimationFrame(function () {
update(callback);
});
console.log(this);
callback();
}
setup();
update(a.draw);
I found error occurs in the update(a.draw). It cannot access the properties of a in this line of code.
I wonder if this is a javascript scope problem?
Thanks.
You need to create a reference to the context you want to refer to inside the callback before you call requestAnimationFrame, just like you would with a normal event callback.
Var that = this;
requestAnimationFrame(function(){
that.doSimething();
});

Javascript: Function overwrite with passing arguments [duplicate]

This question already has answers here:
Passing arguments forward to another javascript function
(5 answers)
Closed 8 years ago.
I am using Winston (Logger for nodejs).
In my script i call:
log.error("my error", {input: [1,2]});
console show me:
error: my error input=[1, 2]
What i want to do: don' call at log.error(null)
In some functions i dynamically call the log.error with a variable and dont know if the variable are NULL or have a value. If i call log.error(null) he write "error:" to the console.
I tried with this:
log.error = (function () {
var old = log.error;
return function newerror() {
if (arguments.length !== 0) {
old(arguments);
}
};
})();
But now i got:
error: 0=my error, input=[1, 2]
as output.
My Question
how i can call the log.error() function with die arguments given (arguments-var in javascript is a object).
Function style:
function foo(arg1,arg2,...)
I want something like:
foo.call(myargumentsobj);
oh i got it:
old.apply(this, arguments);

when is 'this' available? [duplicate]

This question already has answers here:
How does "this" keyword work within a function?
(7 answers)
Closed 8 years ago.
I have a JS object that in pseudo code is doing this:
function myObj {
// in constructor
... initialise vars
... call $.Ajax to fetch view from server
function AjaxCallBack() {
// load the DOM with the HTML result
$('#dialog').html(ajax.result);
// try to register a change handler
$("#dialog :input").change(this.formChangeEvent);
}
this.formChangeEvent = function(){
... do stuff
}
}
The problem is that in the AjaxCallBack, 'this' is not myObj object, but rather the 'window' object and can't resolve the function
The only way I've been able to get around this problem is to have the receiver of the object call back into a separate function of myObj and register the event
objInstance = new myObj();
objInstance.registerEventHandler();
I've tried a few other things, but I'm obviously missing something basic.
If I understand your question then you can hold this to any variable like var thisObj=$(this) . Now you can use thisObj.formChangeEvent. Hope this will help.
Try this:
function myObj {
// in constructor
... initialise vars
... call $.Ajax to fetch view from server
var oThis = this; // oThis is object this which points to myObj
function AjaxCallBack() {
// load the DOM with the HTML result
$('#dialog').html(ajax.result);
// try to register a change handler
$("#dialog :input").change(oThis.formChangeEvent);
}
this.formChangeEvent = function(){
... do stuff
}
}
It will do the trick.

Categories

Resources