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