Javascript面向对象及组件详细介绍(四)包装对象

来源:互联网 发布:砍价软件有哪些 编辑:程序博客网 时间:2024/05/18 05:11

包装对象包装对象包装对象包装对象包装对象

  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5. <title>无标题文档</title>  
  6. <script>  
  7.   
  8. /*function Aaa(){  
  9.     this.name = '小明';  
  10. }  
  11. Aaa.prototype.showName = function(){  
  12.     alert( this.name );  
  13. };  
  14.   
  15. var a1 = new Aaa();  
  16. a1.showName();  
  17.   
  18.   
  19. var arr = new Array();  
  20. arr.push();  
  21. arr.sort();  
  22.   
  23. //在JS源码 : 系统对象也是基于原型的程序  
  24.   
  25. function Array(){  
  26.     this.lenglth = 0;  
  27. }  
  28. Array.prototype.push = function(){};  
  29. Array.prototype.sort = function(){};*/  
  30.   
  31.   
  32. //尽量不要去修改或者添加系统对象下面的方法和属性  
  33.   
  34. var arr = [1,2,3];  
  35.   
  36. Array.prototype.push = function(){  
  37.       
  38.     //this : 1,2,3  
  39.     //arguments : 4,5,6  
  40.       
  41.     for(var i=0;i<arguments.length;i++){  
  42.         this[this.length] = arguments[i]  
  43.     }  
  44.       
  45.     return this.length;  
  46. };  
  47.   
  48. arr.push(4,5,6);  
  49.   
  50. alert( arr );  
  51.   
  52. //pop shift unshift splice sort  
  53.   
  54. </script>  
  55. </head>  
  56.   
  57. <body>  
  58. </body>  
  59. </html>  


  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5. <title>无标题文档</title>  
  6. <script>  
  7.   
  8. /*var str = 'hello';  
  9.   
  10. alert( typeof str );  
  11.   
  12. str.charAt(0);  
  13. str.indexOf('e');*/  
  14.   
  15. //null undefined  
  16. //包装对象 : 基本类型都有自己对应的包装对象 : String  Number  Boolean   
  17.   
  18. /*var str = new String('hello');  
  19.   
  20. //alert( typeof str );  
  21.   
  22. alert(str.charAt(1));  
  23.   
  24. String.prototype.charAt = function(){};*/  
  25.   
  26.   
  27.   
  28. //var str = 'hello';  
  29. //str.charAt(0);  //基本类型会找到对应的包装对象类型,然后包装对象把所有的属性和方法给了基本类型,然后包装对象消失  
  30.   
  31.   
  32. /*var str = 'hello';  
  33.   
  34. String.prototype.lastValue = function(){  
  35.     return this.charAt(this.length-1);  
  36. };  
  37.   
  38. alert( str.lastValue() );  //o*/  
  39.   
  40.   
  41. var str = 'hello';  
  42.   
  43. str.number = 10;  
  44.   
  45. alert( str.number );  //undefined  
  46.   
  47. </script>  
  48. </head>  
  49.   
  50. <body>  
  51. </body>  
  52. </html>  




0 0
原创粉丝点击