javascript學習筆記—函數的提示

來源:互聯網
上載者:User
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD>  <TITLE> New Document </TITLE>  <META NAME="Generator" CONTENT="EditPlus">  <META NAME="Author" CONTENT="">  <META NAME="Keywords" CONTENT="">  <META NAME="Description" CONTENT="">  <script type="text/javascript">//裴波那數字function text(num){if(num==0){return 0;}if(num==1){return 1;}return arguments.callee(num-1)+arguments.callee(num-2); //遞迴調用}/* 定義函數的四種方式:關鍵字:function方法一:function 函數名([參數列表]){}方法二:var 函數名=function([參數列表]){}方法三:var 函數名=function 函數名([參數列表]){}方法四:(很少用到,可讀性差)var 函數名=new Function([參數列表],body);沒有返回的函數,傳回值是undefined;函數,有一個屬性length,表達定義函數時的參數的個數函數內部,有一個屬性arguments,表示執行函數時,真正傳遞的函數的類數組的對象arguments有length屬性,表示運行時傳遞的參數的個數arguments有callee屬性,它指向對象本身函數,有一個屬性prototype,表示函數的原型,它是一個對象。當我們將函數做構造器,建立一個對象時,解析器會將prototype屬性所指向原型對象中的所有的屬性和方法都拷貝到新建立的對象中。如果儲存對象狀態或私人屬性或方法,應該放在函數內部。如果是通用的,公用的方法,應該放在函數的原型中。this,與其它物件導向語言不同是,javascript中的this指標不一定指向當前對象。call(),apply()方法:指定this指向。指定函數執行內容對象。call(內容物件,[參數1,參數2,。。。。])apply(內容物件,[參數數組])*///不常用的函式宣告方法,var test = new Function("a","b","alert('sum:'+(a+b));");var test = new Function("a,b","alert('sum:'+(a+b));"); //效果同上test(1,2);function fun(){};//函數作為對象來使用//為函數對象fun添加屬性fun.a = "a";//為函數對象fun添加方法fun.b = function(){alert("b()");}alert(fun.a);fun.b();//函數作為構造器來使用function Person(){this.name = "張三";//添加屬性//添加方法this.show = function(){alert("name:"+this.name);}}var p = new Person();alert(p instanceof Person);alert(p.name);p.show();//類比存取權限function Person(){this.name = "張三";//publicvar age = 22;//private//添加方法this.show = function(){alert("name:"+this.name);}//提供public方法來訪問privatethis.getAge = function(){return age;}}//匿名函數的調用(function(name,age,gender){this.name=name;this.age=age;this.gender=gender;alert(this.name+","+this.age+","+this.gender);})("續寫經典","18","男");//函數的調用(function sum(a,b,c,d){alert(a+b+c+d);alert(sum.length); //定義函數的參數的個數alert(arguments.length); //實際傳遞參數的個數})(1,2,4,5,6);function Person(name,age){this.name = name;this.age = age;}//為Person的原型對象添加屬性Person.prototype.sex = "男";//為Person的原型對象添加方法Person.prototype.show = function(){alert(this.name+","+this.age+","+this.sex);}//查看原型中的屬性和方法var pt = Person.prototype;for(var pro in pt){alert(pro);}//為object的原型添加公用的(取最大值)的方法Object.prototype.getMax = function(arr){if(!arr instanceof Array){alert("必須是數組");}else{for(var i=1,max = arr[0];i<arr.length;i++){if(max < arr[i]){max = arr[i];}}return max;}}var arr = [54,1,654,14,21];var o = new String();alert(o.getMax(arr));//建立兩個對象var p1 = {name:"p1"};//new Object();簡寫var p2 = {name:"p2"};function show(msg){if(msg){alert(msg+","+this.name);//預設this == window}else{alert(name);}}show.call(p1,["this指向p1"]);show.call(p2,["this指向p2"]);show.call(window);//this指向對象的簡單分析function p1(){this.name = "我是對象p1";this.show = function(){return this.name;//this指向的是本身的name屬性};}function p(){this.show = function(){return this.name;//this指向的是window對象的name屬性};}var a = new p();document.write(a.show()+"<br />");var b = new p1();document.write(b.show()+"<br />");/*JavaScript反射機制:反射機制是指程式在運行期間能夠擷取自身的資訊,例如一個對象能夠在運行時知道自己擁有哪些方法和屬性,並且可以調用這些方法和屬性。在C#和Java中都提供了反射機制,能夠在運行時動態判斷和調用自己的屬性或方法。在JavaScript中可用for(…in…)語句來實現反射*/function stu(){this.one="續寫經典";this.two="新的一天,qy會更好更專業!";}stu.prototype.showStu=function(){alert("我是:"+this.one+"\n簽名:"+this.two);}var student=new stu();student.showStu();//反射的簡單應用for(var s in student){if(typeof(student[s])=="function"){student[s]();}else{alert(student[s]);}}/*利用共用prototype實現繼承類之間的繼承關係是現實世界中遺傳關係的直接類比,它表示類之間的內在聯絡以及對屬性和操作的共用,即子類可以沿用父類(被繼承的類)的某些特徵。當然子類也可以具有自己獨立的屬性和操作。繼承也是軟體複用的一種形式,新類由已存在的類產生,通過保留它們的屬性和行為並且根據新類的要求對效能加以修改,添加新的屬性和行為。雖然在JavaScript中沒有專門的機制來實作類別的繼承,但可以通過複製一個類的prototype到另外一個類來實現繼承。下面是一種簡單的繼承實現,代碼如下:*/function HelloClass(){//構造方法}function HelloSubClass(){//構造方法}HelloSubClass.prototype=HelloClass.prototype;HelloSubClass.prototype.Propertys="name";HelloSubClass.prototype.subMethods=function(){//方法實現代碼alert("in Methods");}var obj=new HelloSubClass();obj.subMethods();/* 在以上的代碼中,首先是HelloSubClass具有了和HelloClass一樣的prototype,如果不考慮構造方法,則兩個類是等價的。隨後,又通過prototype給HelloSubClass賦予了額外的屬性和方法所以HelloSubClass是在HelloClass的基礎上增加了新的屬性和方法,從而實現了類的繼承。 */  </script> </HEAD> <BODY>   </BODY></HTML>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.