15
JavaScript Function Jason

Javascript function

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Javascript function

JavaScript FunctionJason

Page 2: Javascript function

Javascript - length Specifies the number of arguments

expected by the function.

Syntax› functionName.length

Page 3: Javascript function

Javascript - length console.log( (function () {}).length ); console.log( (function (a) {}).length ); console.log( (function (a, b)

{}).length );

Page 4: Javascript function

Javascript - length function ArgTest(a, b){ var s = ""; s += "Expected Arguments: " +

ArgTest.length; s += "<br />"; s += "Passed Arguments: " +

arguments.length; return s; }

Page 5: Javascript function

Javascript - call Calls a function with a given this value and

arguments provided individually.

Syntax› fun.call(thisArg[, arg1[, arg2[, ...]]])

Page 6: Javascript function

Javascript - call function diplayInfo(year, month, day){ return "Name:" + this.name + ";birthday:" +

year + "." + month + "." + day;

}

var p = { name: "Jason" };

diplayInfo.call(p, 1985, 11, 5);

Page 7: Javascript function

Javascript - apply Calls a function with a given this value

and arguments provided as an array

Syntax› fun.apply(thisArg[, argsArray])

Page 8: Javascript function

Javascript - apply function diplayInfo(year, month, day){ return "Name:" + this.name + ";birthday:" +

year + "." + month + "." + day; }

var p = { name: "Jason" };

console.log(diplayInfo.apply(p, [1985, 11, 5]));

Page 9: Javascript function

Javascript - callee Specifies the currently executing function

callee is a property of the arguments object.

Syntax› [function.]arguments.callee

Page 10: Javascript function

Javascript - callee function factorial(n){ if (n <= 0) return 1; else return n * arguments.callee(n - 1); }

factorial(4);

Page 11: Javascript function

Javascript - bind Creates a new function that, when

called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Syntax

fun.bind(thisArg[, arg1[, arg2[, ...]]])

Page 12: Javascript function

Javascript - bind var x = 9; var module = { x: 81, getX: function() { return this.x; } };

module.getX(); //Answer: ?

var getX = module.getX; getX(); //Answer: ?

var boundGetX = getX.bind(module); boundGetX(); //Answer: ? module.x = 100; boundGetX(); //Answer: ?

Page 13: Javascript function

Javascript - bind var checkNumericRange = function (value) { return value >= this.min && value <= this.max; }

var range = { min: 10, max: 20 };

var boundCheckNumericRange = checkNumericRange.bind(range);

var result = boundCheckNumericRange (12); Result = ??

Page 14: Javascript function

Javascript - bind var displayArgs = function (val1, val2, val3, val4) { console.log(val1 + " " + val2 + " " + val3 + " " +

val4); }

var emptyObject = {};

var displayArgs2 = displayArgs.bind(emptyObject, 12, "a");

displayArgs2("b", "c"); //Answer: ?

Page 15: Javascript function

Reference Javascript MDN

› https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function

Javascript MSDN› http://teamserver:8080/tfs/DefaultCollection/!B

acklog/_boards