// getRectangleArea(x1, y1, x2, y2) // getRectangleArea(width, height) function getRectangleArea(x1, y1, x2, y2) { if (arguments.length==2) return x1*y1; return (x2-x1)*(y2-y1); } * This source code was highlighted with Source Code Highlighter .
// getRectangleArea(x1, y1, x2, y2) // getRectangleArea(width, height) function getRectangleArea(x1, y1, x2, y2) { if (arguments.length==2) return x1*y1; return (x2-x1)*(y2-y1); } * This source code was highlighted with Source Code Highlighter .
// getRectangleArea(x1, y1, x2, y2) // getRectangleArea(width, height) function getRectangleArea(x1, y1, x2, y2) { if (arguments.length==2) return x1*y1; return (x2-x1)*(y2-y1); } * This source code was highlighted with Source Code Highlighter .
// getRectangleArea(x1, y1, x2, y2) // getRectangleArea(width, height) function getRectangleArea(x1, y1, x2, y2) { if (arguments.length==2) return x1*y1; return (x2-x1)*(y2-y1); } * This source code was highlighted with Source Code Highlighter .
// getRectangleArea(x1, y1, x2, y2) // getRectangleArea(width, height) function getRectangleArea(x1, y1, x2, y2) { if (arguments.length==2) return x1*y1; return (x2-x1)*(y2-y1); } * This source code was highlighted with Source Code Highlighter .
// getRectangleArea(x1, y1, x2, y2) // getRectangleArea(width, height) function getRectangleArea(x1, y1, x2, y2) { if (arguments.length==2) return x1*y1; return (x2-x1)*(y2-y1); } * This source code was highlighted with Source Code Highlighter .
// getRectangleArea(x1, y1, x2, y2) // getRectangleArea(width, height) function getRectangleArea(x1, y1, x2, y2) { if (arguments.length==2) return x1*y1; return (x2-x1)*(y2-y1); } * This source code was highlighted with Source Code Highlighter .
// getRectangleArea(x1, y1, x2, y2) // getRectangleArea(width, height) function getRectangleArea(x1, y1, x2, y2) { if (arguments.length==2) return x1*y1; return (x2-x1)*(y2-y1); } * This source code was highlighted with Source Code Highlighter .
* This source code was highlighted with Source Code Highlighter .
- function getRectangleArea (width, height) {
- Return width * height;
- }
- function getRectangleArea (x1, y1, x2, y2) {
- return (x2-x1) * (y2-y1);
- }
functionName.length
, and the number of arguments passed by - by arguments.length
. Recall also the great thing apply
and write:* This source code was highlighted with Source Code Highlighter .
- function polymorph () {
- var len2func = [];
- for ( var i = 0; i <arguments.length; i ++)
- if ( typeof (arguments [i]) == "function" )
- len2func [arguments [i] .length] = arguments [i];
- return function () {
- return len2func [arguments.length] .apply ( this , arguments);
- }
- }
polymorph
function takes as arguments a set of subfunctions with a different number of parameters, puts them into an array of len2func
, whose index is the number of parameters, and returns a closure function that, depending on the number of arguments, causes a particular subfunction. Use this:* This source code was highlighted with Source Code Highlighter .
- var getRectangleArea2 = polymorph (
- function (width, height) {
- Return width * height;
- },
- function (x1, y1, x2, y2) {
- return (x2-x1) * (y2-y1);
- }
- );
getRectangleArea2
is a complete analogue of getRectangleArea
, however, the code has become much more transparent, and now a comment is no longer required: the ways of using are obvious.typeof
and instanceof
, so it is easy to extend the polymorph()
function for these cases as well. Types of parameters will be set in a special object before the function:* This source code was highlighted with Source Code Highlighter .
- var PolyFunc = polymorph (
- function (a, b, c) {
- return "Three arguments version - any types" ;
- },
- {i: Number, str: String},
- function (i, str) {
- return "Number and string passed" ;
- },
- {re: RegExp},
- function (re, a) {
- return "RegExp and something else passed" ;
- },
- {f: Function, b: Boolean},
- function (f, b) {
- return "Function and boolean passed" ;
- },
- {f: Function, i: Number},
- function (f, i) {
- return "Function and number passed" ;
- }
- );
- alert (PolyFunc (1,2,3)); // "Three arguments version - any types"
- alert (PolyFunc (1, "qq" )); // "Number and string passed"
- alert (PolyFunc ( function () {}, true )); // "Function and boolean passed"
- alert (PolyFunc ( function () {}, 1)); // "Function and number passed"
- alert (PolyFunc (/ a /, 1)); // "RegExp and something else passed"
- alert (PolyFunc (/ a /, "str" )); // "RegExp and something else passed"
Source: https://habr.com/ru/post/86403/
All Articles