X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/6746dc89c47ed01b165cc1152533605f97eb8e8d..f562e4c6e5fac7bcb445985b99acbea4d706e6f0:/docs/output/Function.js diff --git a/docs/output/Function.js b/docs/output/Function.js new file mode 100644 index 00000000..32ca5e95 --- /dev/null +++ b/docs/output/Function.js @@ -0,0 +1 @@ +Ext.data.JsonP.Function({"tagname":"class","html":"
Files
Every function in JavaScript is actually a Function
object.
Function
objects created with the Function
constructor are parsed when the\nfunction is created. This is less efficient than declaring a function and\ncalling it within your code, because functions declared with the function\nstatement are parsed with the rest of the code.
All arguments passed to the function are treated as the names of the\nidentifiers of the parameters in the function to be created, in the order in\nwhich they are passed.
\n\nInvoking the Function
constructor as a function (without using the new
\noperator) has the same effect as invoking it as a constructor.
Function
constructorThe following code creates a Function
object that takes two arguments.
// Example can be run directly in your JavaScript console\n\n// Create a function that takes two arguments and returns the sum of those\narguments\nvar adder = new Function(\"a\", \"b\", \"return a + b\");\n\n// Call the function\nadder(2, 6);\n// > 8\n
\n\nThe arguments \"a\" and \"b\" are formal argument names that are used in the\nfunction body, \"return a + b\".
\n\nCreates new Function object.
\nNames to be used by the function as formal argument names. Each must be a\nstring that corresponds to a valid JavaScript identifier or a list of such\nstrings separated with a comma; for example \"x
\", \"theValue
\", or \"a,b
\".
A string containing the JavaScript statements comprising the function\ndefinition.
\nApplies the method of another object in the context of a different object (the\ncalling object); arguments can be passed as an Array object.
\n\nYou can assign a different this object when calling an existing function. this
refers to the\ncurrent object, the calling object. With apply
, you can write a method once and then inherit it\nin another object, without having to rewrite the method for the new object.
apply
is very similar to call, except for the type of arguments it supports. You can use an\narguments array instead of a named set of parameters. With apply, you can use an array literal, for\nexample, fun.apply(this, ['eat', 'bananas'])
, or an Array object, for example, fun.apply(this,\nnew Array('eat', 'bananas'))
.
You can also use arguments for the argsArray
parameter. arguments
is a local variable of a\nfunction. It can be used for all unspecified arguments of the called object. Thus, you do not have\nto know the arguments of the called object when you use the apply
method. You can use arguments\nto pass all the arguments to the called object. The called object is then responsible for handling\nthe arguments.
Since ECMAScript 5th Edition you can also use any kind of object which is array like, so in\npractice this means it's going to have a property length and integer properties in the range\n[0...length)
. As an example you can now use a NodeList or a own custom object like {'length': 2,\n'0': 'eat', '1': 'bananas'}
.
You can use apply
to chain constructors for an object, similar to Java. In the following example,\nthe constructor for the Product
object is defined with two parameters, name
and value
. Two\nother functions Food
and Toy
invoke Product
passing this
and arguments
. Product
\ninitializes the properties name
and price
, both specialized functions define the category. In\nthis example, the arguments
object is fully passed to the product constructor and corresponds to\nthe two defined parameters.
function Product(name, price) {\n this.name = name;\n this.price = price;\n\n if (price < 0)\n throw RangeError('Cannot create product \"' + name + '\" with a negative price');\n return this;\n}\n\nfunction Food(name, price) {\n Product.apply(this, arguments);\n this.category = 'food';\n}\nFood.prototype = new Product();\n\nfunction Toy(name, price) {\n Product.apply(this, arguments);\n this.category = 'toy';\n}\nToy.prototype = new Product();\n\nvar cheese = new Food('feta', 5);\nvar fun = new Toy('robot', 40);\n
\n\nClever usage of apply
allows you to use built-ins functions for some tasks that otherwise\nprobably would have been written by looping over the array values. As an example here we are going\nto use Math.max/Math.min to find out the maximum/minimum value in an array.
//min/max number in an array\nvar numbers = [5, 6, 2, 3, 7];\n\n//using Math.min/Math.max apply\nvar max = Math.max.apply(null, numbers); // This about equal to Math.max(numbers[0], ...) or\n// Math.max(5, 6, ..)\nvar min = Math.min.apply(null, numbers);\n\n//vs. simple loop based algorithm\nmax = -Infinity, min = +Infinity;\n\nfor (var i = 0; i < numbers.length; i++) {\nif (numbers[i] > max)\n max = numbers[i];\nif (numbers[i] < min)\n min = numbers[i];\n}\n
\n\nBut beware: in using apply
this way, you run the risk of exceeding the JavaScript engine's\nargument length limit. The consequences of applying a function with too many arguments (think more\nthan tens of thousands of arguments) vary across engines, because the limit (indeed even the nature\nof any excessively-large-stack behavior) is unspecified. Some engines will throw an exception. More\nperniciously, others will arbitrarily limit the number of arguments actually passed to the applied\nfunction. (To illustrate this latter case: if such an engine had a limit of four arguments [actual\nlimits are of course significantly higher], it would be as if the arguments 5, 6, 2, 3 had been\npassed to apply in the examples above, rather than the full array.) If your value array might grow\ninto the tens of thousands, use a hybrid strategy: apply your function to chunks of the array at a\ntime:
function minOfArray(arr)\n{\n var min = Infinity;\n var QUANTUM = 32768;\n for (var i = 0, len = arr.length; i < len; i += QUANTUM)\n {\n var submin = Math.min.apply(null, numbers.slice(i, Math.min(i + QUANTUM, len)));\n min = Math.min(submin, min);\n }\nreturn min;\n}\n\nvar min = minOfArray([5, 6, 2, 3, 7]);\n
\nThe value of this provided for the call to fun. Note that this may not be\nthe actual value seen by the method: if the method is a function in non-strict mode code, null and\nundefined will be replaced with the global object, and primitive values will be boxed.
\nAn array like object, specifying the arguments with which fun should be\ncalled, or null or undefined if no arguments should be provided to the function.
\nReturns what the function returns.
\nCalls (executes) a method of another object in the context of a different\nobject (the calling object); arguments can be passed as they are.
\n\nYou can assign a different this object when calling an existing function. this
refers to the\ncurrent object, the calling object.
With call
, you can write a method once and then inherit it in another object, without having to\nrewrite the method for the new object.
You can use call to chain constructors for an object, similar to Java. In the following example,\nthe constructor for the product object is defined with two parameters, name and value. Another\nobject, prod_dept
, initializes its unique variable (dept
) and calls the constructor for\nproduct
in its constructor to initialize the other variables.
function Product(name, price) {\n this.name = name;\n this.price = price;\n\n if (price < 0)\n throw RangeError('Cannot create product \"' + name + '\" with a negative price');\n return this;\n}\n\nfunction Food(name, price) {\n Product.call(this, name, price);\n this.category = 'food';\n}\nFood.prototype = new Product();\n\nfunction Toy(name, price) {\n Product.call(this, name, price);\n this.category = 'toy';\n}\nToy.prototype = new Product();\n\nvar cheese = new Food('feta', 5);\nvar fun = new Toy('robot', 40);\n
\n\nIn this purely constructed example, we create anonymous function and use call
to invoke it on\nevery object in an array. The main purpose of the anonymous function here is to add a print\nfunction to every object, which is able to print the right index of the object in the array.\nPassing the object as this
value was not strictly necessary, but is done for explanatory purpose.
var animals = [\n{species: 'Lion', name: 'King'},\n{species: 'Whale', name: 'Fail'}\n];\n\nfor (var i = 0; i < animals.length; i++) {\n (function (i) {\n this.print = function () {\n console.log('#' + i + ' ' + this.species + ': ' + this.name);\n }\n}).call(animals[i], i);\n}\n
\nThe value of this provided for the call to fun
.Note that this may not be\nthe actual value seen by the method: if the method is a function in non-strict mode code, null
\nand undefined
will be replaced with the global object, and primitive values will be boxed.
Arguments for the object.
\nReturns what the function returns.
\nReturns a string representing the source code of the function. Overrides the\nObject.toString
method.
The Function object overrides the toString
method of the Object object; it does\nnot inherit Object.toString. For Function
objects, the toString
method returns a string\nrepresentation of the object.
JavaScript calls the toString
method automatically when a Function
is to be represented as a\ntext value or when a Function is referred to in a string concatenation.
For Function
objects, the built-in toString
method decompiles the function back into the\nJavaScript source that defines the function. This string includes the function
keyword, the\nargument list, curly braces, and function body.
The function as a string.
\n