JS Prototype understanding

Source: Internet
Author: User

Simple comprehension: Prototype object is an important mechanism to implement object-oriented. Each function is also an object, and their corresponding class is

function, each of which has a sub-object prototype. Prototype represents the prototype of the function,

Prototype represents a collection of properties for a class. When you create an object of a class by using new, the genus of the prototype object

will become the property of the instantiated object. (similar to reflection)


PS. (in JS is all objects, class function is the top-level superclass of function, function instantiation called object, when the function is not defined by the instance, in fact, this definition belongs to function)

  
  1. <script>
  2. /*
  3. * 关于prototype,理解这个很有必要
  4. * 可以在类型上使用proptotype来为类型添加行为。这些行为只能在类型的实例上体现。
  5. * JS中允许的类型有Array, Boolean, Date, Enumerator, Error, Function, Number, Object, RegExp, String
  6. * 以后这样分,没有实例化的类称为类型,实例化的类称为对象实例简称实例
  7. */
  8. Object.prototype.name = "zhangsan";
  9. Object.prototype.nihao = function(){
  10. alert("i can method name is "+this.name);
  11. }
  12. var obj = new Object();
  13. obj.nihao();
  14. alert(obj.name);
  15. //在实例上不能使用prototype,否则会发生编译错误
  16. obj.prototype.sex = "男";//error,无法给一个实例prototype
  17. var o = {
  18. name:"zhangsan"
  19. }
  20. o.prototype.age = 30;//error,无法给一个实例prototype
  21. //可以为类型定义“静态”的属性和方法,直接在类型上调用即可
  22. alert(Object.name);
  23. Object.nihao();
  24. //实例不能调用类型的静态属性或方法,否则发生对象未定义的错误。
  25. Object.class = "三年二班";//类静态属性
  26. var ob = new Object();
  27. alert(ob.class); //error 实例不能调用类型的静态属性和方法
  28. //可以在外部使用prototype为自定义的类型添加属性和方法。
  29. function Mytest(){
  30. this.name = "zhangsan";
  31. this.age = 20;
  32. }
  33. Mytest.prototype.hello = function(){
  34. alert(this.name);
  35. }
  36. var m = new Mytest();
  37. m.hello();
  38. //在外部不能通过prototype改变自定义类型的属性或方法。
  39. //该例子可以看到:调用的属性和方法仍是最初定义的结果。
  40. Mytest.prototype.name = "lisi";
  41. var mm = new Mytest();
  42. alert(mm.name);
  43. //可以在对象实例上改变或增加属性。(这个是肯定的)
  44. //也可以在对象上改变或增加方法。(和普遍的面向对象的概念不同)
  45. mm.name2 = "lisi";
  46. mm.hello = function(){
  47. alert(this.name2);
  48. }
  49. //mm.hello();
  50. //继承,这个例子说明了一个类型如何从另一个类型继承。
  51. function Mytest2(){}
  52. Mytest2.prototype = new Mytest;
  53. var m2 = new Mytest2();
  54. alert(m2.name);
  55. //这个例子说明了子类如何重写父类的属性或方法。
  56. Mytest2.prototype.name = "wangwu";
  57. Mytest2.prototype.hello = function(){
  58. alert(‘i can mytest2 extend Mytest save method hello‘);
  59. }
  60. var m3 = new Mytest2();
  61. m3.hello();
  62. //子类中的name属性值不会被父类覆盖
  63. function Mytest3(){
  64. this.name = "子类中的name属性值不会被父类覆盖";
  65. this.age = 20;
  66. }
  67. Mytest3.prototype = new Mytest();
  68. var m4 = new Mytest3();
  69. alert(m4.name);
  70. </script>


JS Prototype understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.