// Programming JavaScript Applications
// Chapter 2
// Method context
// take care when this function is invoked as function, without cutoff
// then this will pollute the global namespace
function highPass(number, cutoff) {
cutoff = cutoff || this.cutoff;
return (number >= cutoff);
}
var filter1 = {
highPass: highPass,
cutoff: 5
},
filter2 = {
// no highpass here
cutoff: 3
};
QUnit.test("Invoking a function", function (assert) {
expect(5);
var result = highPass(6, 5); // function invokation
var result2 = highPass(5, 6); // -"-
var result3 = filter1.highPass(3); // method invokation
var result4 = highPass.call(filter2, 3); // call: call any method/function on any object
var result5 = filter1.highPass(6);
assert.equal(result, true, "6 >= 5 should be true");
assert.equal(result2, false, "5 >= 6 should be false");
assert.equal(result3, false, "3 >= 5 should be false");
assert.equal(result4, true, "3 >= 3 should be true");
assert.equal(result5, true, "6 >= 6 should be true");
});
QUnit.test("Using apply test", function(assert) {
// apply is used for array like objects
var numbers = [1,2,3,4,5];
var maxNum = Math.max.apply(null, numbers);
assert.equal(maxNum, 5, "the biggest number should be 5");
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.