The function object in JS

Source: Internet
Author: User

JavaScript three defines a function method:

{} represents the body of the function
1. The first is to use function statements to define functions

function func () {}


2. The second type uses the function () constructor to define functions (not commonly used)
var functions name = new function ("Parameter 1", "Parameter 2", "Parameter 3" ... "functional body");

var sum = new Function ("X", "Y", "var z=x+y;return z;");

3. The third type is to define the function in an expression


var function name = function (parameter 1, parameter 2, ...) {function Body};

var add = function (A, b) {return a+b;}

Calling functions
document.write (Add (50,20));

code example:

<script type= "Text/javascript" >//using constructors to create built-in objects     varobj =NewObject ();//Creating ObjectsObj.name = "Fan Bingbing"; Obj.age= 18; Obj.getinfo=function(){          return  This. name+ ":" + This. Age;//This current     }     //Usedocument.write ("Name property in the object:" +obj.name+ "<br/>"); document.write ("Method of object:" +obj.getinfo ()); //2. Create a custom object directly     varObj1 = {name: ' Miss Zhang ', Age:18,ningpingai:function(){return  This. name+ "I'll screw the cap.";}}; document.write (The Name property in the object: "+obj1.name+" <br/> "); document.write ("Object Miss Zhang's method:" +obj1.ningpingai () + "<br/>"); //create an object using a custom constructor    functionObj2 (name,age) { This. Name =name;  This. Age =Age ;  This. say=function(){             return"I am:" + This. name+ "I am very handsome, very handsome."; }    }    //instantiating an object    varOB =NewOBJ2 ("int", 18); varf =NewObj2 ("for", 45); document.write (The Name property in the object: "+ob.name+" <br/> "); document.write ("Object's Method:" +ob.say () + "<br/>"); document.write (The Name property in the object: "+f.name+" <br/> "); document.write ("Object's Method:" +f.say () + "<br/>"); //View the format of an objectalert (ob.constructor); /*job using loop 99 multiplication table Four write a simple carousel diagram*/</script>

The function object in JS

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.