3 ways to define a JavaScript method

Source: Internet
Author: User

 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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.