实践

  1. // 用new建立一个简单的对象
  2. /*
  3. var t = new Object();
  4. t.a = "test";
  5. t.b = function (){
  6. alert(t.a);
  7. }
  8. t.b();
  9. // 用new和this关键字改进
  10. var t = new Object();
  11. t.a = "test";
  12. t.b = function(){
  13. alert(this.a);
  14. }
  15. t.b();
  16. // 改进
  17. function test(){
  18. this.a = "test";
  19. this.b = function(){
  20. alert(this.a);
  21. }
  22. return 1;
  23. }
  24. var t = test();
  25. alert(a);
  26. b();
  27. alert(t);
  28. alert(t.a);
  29. t.b;
  30. // 直接调用函数时,t只是函数的返回值,并不是需要的对象。
  31. // 由于test是window对象调用的,this指代了window对象,把a和b赋给了window对象。
  32. function test(){
  33. this.a = "tetst";
  34. this.b = function(){
  35. alert(this.a);
  36. }
  37. return 1;
  38. }
  39. var t = new test();
  40. alert(t);
  41. alert(t.a);
  42. t.b();
  43. alert(a);
  44. b();
  45. // 用new运算符时,调用test函数的对象被赋给了t,
  46. // this指代t对象,函数返回值被忽略。a和b()在window上未定义。
  47. function test(){
  48. var t = new Object();
  49. t.a = "test";
  50. t.b = function(){
  51. alert(this.a);
  52. }
  53. return t;
  54. }
  55. var t1 = test();
  56. alert(t1);
  57. alert(t1.a);
  58. t1.b();
  59. var t2 = test();
  60. t2.a = "test2";
  61. t2.b;
  62. alert(Object.a);
  63. Object.b();
  64. // 在函数内部new操作符新建了基本对象并返回,
  65. // 两个test实例不互相影响,也不影响Object对象。
  66. var test_b = function (){
  67. alert(this.a)
  68. };
  69. function test(){
  70. var t = new Object;
  71. t.a = "test";
  72. t.b = test_b;
  73. return t;
  74. }
  75. var t1 = test();
  76. t1.b();
  77. var t2 = test();
  78. t2.a = "test2";
  79. t2.b();
  80. // 上面两个例子的弊端是,b方法其实是只是一个函数指针,
  81. // 两个指针所指向的函数在调用构造函数时即时创建,并非
  82. // 同一个程序实例,造成存储空间浪费。
  83. // 本段代码为改进程序,这样两个实例的b属性为同一个test_b指针,都指向同一个函数。
  84. function test(){
  85. this.a = "test";
  86. }
  87. test.prototype.b = function(){
  88. alert(this.a);
  89. }
  90. test.prototype.c = new Array();
  91. test.prototype.d = 5;
  92. var t1 = new test();
  93. t1.b();
  94. var t2 = new test();
  95. t2.a = "test2";
  96. t2.b();
  97. ///////
  98. t1.c = [1,2];
  99. alert(t1.c);
  100. t2.c[1] = 4;
  101. alert(t2.c);
  102. alert(t1.c);
  103. ///////
  104. t2.d =6;
  105. alert(t2.d);
  106. alert(t1.d);
  107. // 当prototype的属性为对象的引用时,它为指向对象的指针,和普通变量一样被每个实例继承。
  108. // 另外,prototype属性在对象被实例化前创建。也不可以放在函数内,因为那样每次实例化对象时,
  109. // 都会为prototype的属性重新赋值,那样仍然没达到目的。
  110. // 继承
  111. function test(){
  112. this.a = "test";
  113. this.b = function(){
  114. alert(this.a);
  115. }
  116. }
  117. function subTest(){
  118. this.peraP = test;
  119. this.peraP();
  120. this.subName = "subTest";
  121. }
  122. var t1 = new test();
  123. t1.b();
  124. var t2 = new subTest();
  125. t2.b();
  126. alert(t2.subName);
  127. // 在子类的构造函数内,用this调用父类构造函数,实现了继承。
  128. */
  129. function test(){
  130. this.a = "test";
  131. }
  132. test.prototype.b = function(){
  133. alert(this.a);
  134. }
  135. function subTest(){
  136. this.subName = "subTest";
  137. }
  138. subTest.prototype = new test;
  139. subTest.prototype.showName = function(){
  140. alert(this.subName);
  141. }
  142. var t1 = new test();
  143. t1.b();
  144. var t2 = new subTest();
  145. t2.b();
  146. t2.showName();
  147. // 当父类的构造函数不需要传递参数时,可以用
  148. // subClass.prototype = new ParentClass;的方式覆盖子类的prototype原型,
  149. // 子类的新原型属性放在后面添加。
  150. //-----------------
  151. // prototype 属性
  152. // 返回对象类型原型的引用
  153. // objectName.prototype
  154. // objectName 参数是对象的名称。
  155. // 用prototype属性提供对象的类的一组基本功能。
  156. // 对象的新实例“继承”赋予该对象原型的操作。
  157. // 所有的js内部对象都有prototype属性。