how do I compare 2 functions in javascript
var a = b = function( c ){ return c; };
//here, you can use a === b because they're pointing to the same memory and they're the same type
var a = function( c ){ return c; },
b = function( c ){ return c; };
//here you can use that byte-saver Andy E used (which is implicitly converting the function to it's body's text as a String),
''+a == ''+b.
//this is the gist of what is happening behind the scences:
a.toString( ) == b.toString( )
Comparing 2 functions in JavaScript
This fails because each time you do new testFn
it is generating a new compareFn
; you're not pointing to a reference outside of the constructor or inheriting from a prototype.
You can't access a var
iable from inside a Constructor with a shared function, because it doesn't have access to that closure. If you want to achieve a shared function you will need to move these values into a reference-able space too, i.e.
function testFn(name) {
var vars = {}; // using object because javascript passes refs for objects
this.vars = vars; // make `this.vars` point to `vars`
vars.name = name; // set whatever on your object
}
testFn.prototype.compareFn = function () {
console.info(this.vars.name); // can access `vars` through ref of `this.vars`
};
Nowvar a = new testFn('foo'), b = new testFn('bar');
a.compareFn === b.compareFn; // true;
a.compareFn(); // logs "foo"
How to compare two functions that are called with `.bind()`?
I think the best solution here is to store a key alongside your callback inside of the array. This is a lot more reliable and way less hacky than any solution comparing functions:
register: function register(callback, key) {
console.log('register callback');
var obj = {
key: key,
callback: callback
}
callbackList.push(obj);
}
you can then call remove by passing the key desired rather than the callback, and you can compare as such:if(callbackList[0].key === key)
Just make sure you pass the desired key when registering and access the callback property within the array objects where necessary.I think this solution using labels is much cleaner and doesn't require any confusing or strange code.
Can you compare anonymous functions in javascript?
There is no real way to compare 2 different anonymous functions for 'functionality'.
You can check if they are the same object by using your example code.
var func = function () {
alert('hello');
};
alert(func === func);
The above will work, as you are checking to see that both objects are the same.The only other method for comparing is to compare them as a string.
var func1 = function () {
alert("hello");
};
var func2 = function () {
alert('hello');
};
alert(func1.toString() === func2.toString());
Oops!, they are functionally the same, the difference is the quotes used, So this returns false. How can I compare the values between this two functions?
If the values returned by the functions are not primitives you should:
if(!angular.equals(callmecrazier(), callmecrazy()) {
//trigger some other function
}
EDITI am assuming you want to hit api at interval of 10secs and check if there are any changes, right? (else, please clarify what you are trying to achieve). If so you need to chain the async calls and then compare like below:
'use strict';
/**
* @ngdoc function
* @name dashyAppApp.controller:Dashy3Ctrl
* @description
* # Dashy3Ctrl
* Controller of the dashyAppApp
*/
angular.module('dashyAppApp')
.controller('Dashy3Ctrl', function($rootScope, $scope, employees, $interval) {
var _this = this;
$scope.getData = function() { // getDATA function
return employees.getEmployees().then(function(response) {
return response.data;
});
} // End getDATA function
$scope.getData()
.then(function(data1){
$scope.items = data1;
$interval(function() {
$scope.getData()
.then(function(data2){
if (!angular.equals($scope.items, data1) {
//trigger some other function
}
});
}, 10000);
});
});
Compare outcomes of two functions
Define first and second outside the this.checkCanvasWidth() . Functions create scope, so only the function you use with checkCanvasWidth has access to first and second respectively. You have to define them in the scope above those functions, so that the expect function can also see these variables.
zoomIn: {
value: function () {
var first,
second;
this.checkCanvasWidth().then(function (width) {
first = width;
console.log("before:" + width);
});
//performs a click
this.clickZoomIn();
this.checkCanvasWidth().then(function (width) {
second = width;
console.log("after:" + width);
});
expect(first).toBeGreaterThan(second);
}
}
PS: if checkCanvasWidth()
returns a promise, you'll have to rewrite this whole function, since you want to do the expect()
call AFTER first and second ahve been set.Promise version:
zoomIn: {
value: function () {
var first,
second;
this.checkCanvasWidth().then(function (width) {
first = width;
console.log("before:" + width);
if (first && second) {
expect(first).toBeGreaterThan(second);
}
});
//performs a click
this.clickZoomIn();
this.checkCanvasWidth().then(function (width) {
second = width;
if (first && second) {
expect(first).toBeGreaterThan(second);
}
});
}
}
Comparing two functions in JavaScript
You mixed up value
and innerHTML
.
value
is used for input
and textarea
elements and innerHTML
is for almost other element
This code will work for you:
<!DOCTYPE html>
<html>
<body>
<h1>Practice Spelling Test</h1>
<p id="aWord"></p>
<input id="yourTurn">
<button onclick="myFunction()">New Word</button>
<button onclick="checkSpelling()">Check My Spelling</button>
<p id="result"></p>
<script>
var sightWord = ["hoof", "hook", "shook", "hood", "wood", "took", "book", "good", "food", "mood", "look"];
var yourTurn = document.getElementById("yourTurn");
var aWord = document.getElementById("aWord");
function myFunction() {
var showWord = sightWord[Math.floor((Math.random()*10)+1)];
aWord.innerHTML = showWord;
}
function checkSpelling(result) {
var checkWord = (yourTurn.value == aWord.innerHTML)?"Nice Job!":"So close! Try again!";
document.getElementById("result").innerHTML=checkWord;
}
</script>
</body>
</html>
See live code here: http://jsbin.com/ubofus/1/edit
Related Topics
How to Filter an Array in JavaScript
How to Add a "Readonly" Attribute to an <Input>
How to Use Getelementsbyclassname in JavaScript-Function
Create Copy of Multi-Dimensional Array, Not Reference - JavaScript
Why Do I Need to Copy an Array to Use a Method on It
Sending Binary Data in JavaScript Over Http
How to Send Data in Request Body with a Get When Using Jquery $.Ajax()
Using Raw Image Data from Ajax Request for Data Uri
JavaScript Object Literals Syntax Error
Convert JavaScript Object or Array to JSON for Ajax Data
Understanding JavaScript Promises; Stacks and Chaining
How to Parse a Date in Format "Yyyymmdd" in JavaScript
Check If an Object Implements an Interface at Runtime with Typescript