Home > Article > Web Front-end > Detailed explanation of methods and anonymous method instances in Javascript_javascript skills
The examples in this article describe methods and anonymous methods in Javascript. Share it with everyone for your reference. The specific analysis is as follows:
Javascript method (function)
Declaration function
Starts with function, followed by the function name. Unlike C# and Java, Javascript does not need to declare the return value type and parameter type. No return value is undefined.
An example will make it clearer:
Method with no parameters and no return value:
function f1(){ alert('这是一个方法'); } f1();//调用方法
Methods without parameters and return values:
function f2(){ return 100; } var result=f2();//声明一个变量,接收f1()中的返回值 alert(result);//100
Methods with parameters and return values:
function f3(n1,n2){ return n1+n2; } var result=f3(20,30); alert(result);//50
Note 1:
Look at the example first:
function f1(){ alert('这是一个方法'); } alert(f1());//弹出”这是一个方法”后,还会显示undefined
Reason: In js, if a variable is not assigned a value, it is undefined; in this case, f1() has no return value, so it is an unknown value (undefined). The unknown variable here is put into alert(), and of course the pop-up is undefined.
Note 2:
alert(f1);//不写括号,会将f1整个代码以字符串形式显示出来: function f1(){ alert('这是一个方法'); }
There is no method overloading in JavaScript
Only call the latest defined method:
function f1(n1,n2){ alert(n1+n2); } function f1(n1,n2){ alert(n1-n2); } f1(10,2);//8
Conclusion: No matter where it is called, only the latest defined method is called.
Note: Number undefined=undefined
function f1(n1,n2,n3){ alert(n1-n2+n3); } f1(10,2); //NaN,因为没有给n3传值,n3就是undefined, //数字加上undefined还是undefined
The above conclusion: There is no method overloading in Javascript
Note when defining methods:
Do not have the same name of a custom function as the built-in method:
Do not use the same name as the built-in methods in JS or DOM. For example, do not use function names such as selectAll and focus.
Do not have the same name as the system function. (There is a problem when calling your own defined focus method in a click event. It has the same name as the system’s focus() method)
Note on writing rules brackets:
Generally when writing curly braces in js, they are directly followed by
function f1(){ return { age:100}; } var s=f1(); alert(s.age); //undefined。s结果是undefined,undefined.age必然还是undefined
Anonymous method (used a lot)
Why is it recommended to use the anonymous method?
1. There is a method function aa(){alert{'I'm quite handsome'}} in 1.js
2. There is a method function aa(){alert{'I am getting more and more handsome'}} in 2.js
3. Import 1.js and 2.js into index.html in sequence, and call aa(); the result shows: I am getting more and more handsome.
Conclusion: The aa() method in 2.js will override the aa() in 1.js
What to do? No longer specify method name, use anonymous method
Let’s first look at an example of assigning anonymous methods to variables:
var ff=function(n1,n2){ return n1+n2; }; alert(ff(20,30));//50
Write the anonymous method in one line:
Small case: 1:
var x=1; var y=0; var z=0; var add=function (n){n=n+1;return n}; y=add(x);//结果是2,先调用上面add add=function(n){n=n+3;return n;}; z=add(x);//结果是4,调用上面临近的这个add alert(y+','+z);//2,4
Small case 2:
function aa() { alert("aaa"); return function(){alert("bbb");}; } alert(aa);//不写括号,会将aa方法的整个代码显示出来 alert(aa());//aaa,function(){alert("bbb");}; aaa就不解释了,后面那一串是作为aa()的返回值显示 alert(aa()());//aaa,bbb,undefined //下面分解上面这句进行解释 var s=aa();//aaa alert(s());//s()就是function(){alert("bbb");};首先弹出bbb,其次该方法没有返回值,故弹出undefined
I hope this article will be helpful to everyone’s JavaScript programming design.