in the JavaScript world, there are many ways to define a function, which is the embodiment of javascript flexibility, but it is the reason for beginners to be confused,
Especially for students who have no language base. is so-called all the way to Rome, but if the road too much, will let the road people at a loss, because do not know to go that way is the right, hehe, nonsense a big article, gossip less, first look at the code:
/*the first method, using the function statement, has the following format*/functionfn () {alert ("This is a function definition using a functions statement");} fn ();/*the second method, which uses the function () constructor to clone functions*/varF =NewFunction ("A", "B", "Alert (a+b)")); F (A, b); in fact , it is equivalent to the following code:functionF (A, b) {alert (a+b);}/*the third method, using the direct amount of the function*/varZhenn =function() {alert ("Zhenn");} Zhenn ();
function Fn () { /// defines the constructor for this. Elem = "Here is the function () constructor function defined, hehe"; The This function() {alert ("which is defined using the function () constructor, hehe"); }}varnew Fn (); // instantiation of alert (F.elem); F.fn ();
3 ways to define a JavaScript method