this series as effective JavaScript 's reading notes. in the Item , the paper introduces a combination of Apply variable argument list function implemented by method Average , it actually declares only an array as a parameter, but uses the Apply method, you can actually accept several elements as parameters:function Averageofarray (a) {for (var i = 0, sum = 0, n = a.length; i and using arguments variable, y
The following excerpt the source code, in-depth analysis of his design to achieve the idea:
Copy Code code as follows:
function Format (string) {
var args = arguments;
var pattern = new RegExp ("% ([1-" + Arguments.length + "])", "G");
return string (String). replace (pattern, function (match, index) {
return Args[index];
});
};
Through the Format function, the following code:
Copy Code code as follows:
A clever use of JavaScript functions: Assuming that the action has a Jsonobject type of object data, its value may be empty, the foreground JSP page in the JS code would like to directly through the El expression, that is, the form of ${data} to access the value of the object, May cause JS preprocessing error because the data object is empty, such as the JSP has such code, VAR obj=${data}, when the background data is null, the foreground page will bec
Original article: http://handyxuefeng.blog.163.com/blog/static/45452172201162192755468/
The arguments object in the JavaScript function is converted to an array.
The arguments object in the function is called a "class array" object because it has some features of an array: it has the Length attribute and stores parameters for access using a digital index,
The function in JavaScript does not restrict parameters and checks:function does not mind how many parameters are passed in, or what type of arguments are passed in (even if the number of arguments is inconsistent with the definition of function).The parameters in the function are internally saved by the arguments, whi
var v1 = []var v2 = {};var v3 = {};function foo (v1, v2, v3) {V1 = [1];v2 = [2];V3 = {A:3}}Foo (v1, v2, v3);Alert (v1); BlankAlert (v2); [Object Object]alert (V3.A); Undefined/* This shows: v1, V2, V3 have not been changed, V1 is still an array of 0 elements, V2, V3 is still a blank object. (description is not a reference pass)* However, arrays, objects, etc. are passed by value, which refers to the value of the variable address.* The values of arrays, objects, etc., are passed by value differen
This article translates the JavaScript Tutorial playlist of up master Kudvenkat on YouTubeSOURCE Address here:https://www.youtube.com/watch?v=PMsVM7rjupUlist=PL6n9fhu94yhUA99nOsJkKXBqokT3MBK0bThe Javascript parameter (arguments) object is a local variable that is available in all functions. It contains all the arguments
Arguments objectsA special variable arguments can be accessed within each function in JavaScript. This variable maintains all the list of arguments passed into this function.Note: Since arguments has been defined as a variable within a function.So by defining
We all know that JS inside the parameters of the transfer can be different, for example, we have a function: script type = "Text/javascript" > function one (a,b,c) { this .x = A; Console.log (a); } One ( 1 ); script > The number we passed was only one 1, but the formal parameter had ABC three. That's not right, is it?This time if we look at B,c, it will show undefined. PS: for example Console.log (b).Of course, there would have be
JavaScript uses the function keyword to declare functions, you can return a value with return, or you can have no return value.Recommendation: It is convenient to debug the code by either unifying the return value or not unifying the return value.function definition Format:function functionname (parameter) {//body}When defining a function, the parameters can be written or not, JavaScript does not limit the
(1) What is the difference between what is a formal parameter in a JS function and what is an argument? Parameters are called parameter variables, and the variables received by the function in JS are divided into formal parameters and arguments. An argument is a specific data that is actually used to participate in a JS function call. A formal parameter is a variable that receives an argument value when the function is called. According to the actual
About argumentsWhen calling a function, if you need to pass a parameter, the argument is an array, which can be accessed by the built-in object arguments of the function body, such as:ARGUMENTS[0]: first parameterARGUMENTS[1]: Second parameterARGUMENTS[2]: Third parameterARGUMENTS[3]: Fourth parameterOf course, in the name of the function, you can also do not need to name parameters, call arguments directly
The parameter object arguments in Javascript functions is an object, not an array. However, it can access the elements in the array through the following numeric table, and it also has the length attribute to identify the number of its elements. Use the arguments object to verify whether the function parameters are valid.
Script function sum (arg1, arg2) //
(' declaring parameters ' length: ' +arguments.callee.length); Log (' Actual length: ' +arguments.length); Log ( This ); Log (p1); Log (p2); Log (p3);}functionHandlecaller () {MethodA (' xxx ', ' yyy '); Methoda.apply (Handlecaller, [' xxx ', ' yyy ']);//Specify this object}functioninit () {Handlecaller (); Methoda.call (Handlecaller,' xxx ', ' yyy ');//Specify this object}init ();Results:Notice that the first result block of this refers to the Window object, while the other two results r
For people who have learned Java, function overloading is not an unfamiliar concept, but is there a function overload in JavaScript ... And then we're going to test it.This case is modeled after the syntax in Java to simulate function overloading. Let's run the results.Alert (ON):Alert (+/-):Results comparison found that the result of the first run is Nan, and the result of the second run is normal.There is no function overload in
object in JavaScript.
function Argtest (A, b) {var i, S = "The Argtest function expected";var Numargs = arguments.length; Gets the numeric value of the parameter being passed.var Expargs = argtest.length; Gets the numeric value of the expected parameter.if (Expargs S + + Expargs + "argument."ElseS + + Expargs + "arguments."if (Numargs S + + Numargs + "was passed."ElseS + + Numargs + "were passed."s + = "\
method.
For the Apply method
myfunction.apply (obj, arguments).
Use to merge Array.prototype.concat.apply with another array
([1,2,3], arguments)
The ultimate solution, directly into the real array
The following code creates a new array that contains the elements in all arguments objects.
var args = Array.prototype.slice.call (
Note: This article is in the original: http://www.jb51.net/article/30883.htmSelf as study, re-write.First, write in front of the wordsFront-end JavaScript programming is just a little bit of fur, Dom know a little, Jquey will use a little bit, know the solid foundation, understand the front-end of the system of Knowledge for Web development (their main do is PHP background development) important. Of course, what to use, just check what (now learn to s
(1) In the function body, the arguments array object can be used directly, and the elements of this array are all arguments actually passed in when this function is called(2) In fact, when calling a function, the number of arguments passed in can be different from the declaration of the function, the passing of different types of parameters will result in the res
For the question of whether the JavaScript function parameter is a byVal or a pass (BYREF), there is a common misconception that "simple types" such as number,string are values, number, String, Object, array, and so on "complex types." is the address.Isn't that right? Why is there such a misunderstanding? Take a look at these two pieces of code:
Copy Code code as follows:
//The code that creates the illusion of passing value
function Modifylikeb
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.