js中的函數對象

來源:互聯網
上載者:User

標籤:alert   document   blog   函數   object   this   查看   inf   color   

 

JavaScript三種定義函數方法:

{}表示函數體
1.第一種是使用function語句定義函數
     

function func(){}


2.第二種是使用Function()建構函式來定義函數(不常用)
var 函數名 = new Function(“參數1”,”參數2”,”參數3”……”函數體”);

var sum = new Function(”x”,”y”,”var z=x+y;return z;”);

3.第三種是在運算式中定義函數


var 函數名 = function(參數1,參數2,…){函數體};

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

調用函數
document.write(add(50,20));

 

程式碼範例:

<script type="text/javascript">     //使用建構函式建立內建對象     var obj = new Object(); //建立對象     obj.name = "範冰冰";     obj.age = 18;     obj.getinfo = function(){          return this.name+":"+this.age; //this 當前     }     //使用     document.write("對象中的name屬性:"+obj.name+"<br/>");     document.write("對象的方法:"+obj.getinfo());     //2.直接建立自訂對象     var obj1 = {name:‘miss張‘,age:18,ningpingai:function(){return this.name+"我會擰瓶蓋";}};     document.write("對象中的name屬性:"+obj1.name+"<br/>");     document.write("對象miss張的方法:"+obj1.ningpingai()+"<br/>");    //*3.使用自訂建構函式建立對象    function obj2(name,age){         this.name = name;        this.age = age;        this.say=function(){             return "我是:"+this.name+"我很帥,帥的不得了";        }    }    //執行個體化對象    var ob = new obj2("int",18);    var f = new obj2("for",45);    document.write("對象中的name屬性:"+ob.name+"<br/>");    document.write("對象的方法:"+ob.say()+"<br/>");    document.write("對象中的name屬性:"+f.name+"<br/>");    document.write("對象的方法:"+f.say()+"<br/>");    //查看對象的格式    alert(ob.constructor);    /*作業     使用迴圈九九乘法表 四個      寫一個簡單的輪播圖*/    </script>

 

js中的函數對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.