zoukankan      html  css  js  c++  java
  • js 原型

       1:  function Person (name,age) {
       2:              this.name = name;
       3:              this.age = age;
       4:          }
       5:   
       6:          Person.prototype = {
       7:              constructor : Person, // 原型{}这种格式得重新定义指向 要不然默认是Object
       8:              getInfo: function() {
       9:                  return 'A ' + this.name + ' ' + this.age + '.';
      10:              }
      11:          };
      12:   
      13:          var p = new Person('Lily',18);
      14:   
      15:          /**
      16:           * Person的prototype属性指向Person的prototype对象
      17:           * Person的实例p的__proto__属性指向Preson的prototype对象
      18:           * Preson的prototype对象的constructor属性指向Person
      19:           */
      20:          console.log(p.constructor); // Person
      21:          console.log(p.__proto__ == Person.prototype); // true
      22:          console.log(p.__proto__.constructor == Person); // true  p.__proto__ 指向Person的prototype对象 Person的prototype对象constructor属性指向Person构造器
      23:   
      24:          //Object.create EcmaScript5 以上有
      25:          var Person2 = Object.create(p); //复制对象
      26:          console.log(Person2.name);// Lily
      27:          Person2.age = 20;
      28:          console.log(Person2.age);// 20
      29:          console.log(p.age);// 20
      30:   
      31:          var o1 = Object.create({x:1, y:2});
      32:          var o2 = Object.create(Object.prototype);
      33:          console.log(o2);
      34:          console.log(new Object());
       1:  function Person (name,age) {
       2:              this.name = name;
       3:              this.age = age;
       4:          }
       5:   
       6:          Person.prototype = {
       7:              constructor : Person, // 原型{}这种格式得重新定义指向 要不然默认是Object
       8:              getInfo: function() {
       9:                  return 'A ' + this.name + ' ' + this.age + '.';
      10:              }
      11:          };
      12:   
      13:          var p = new Person('Lily',18);
      14:   
      15:          /**
      16:           * Person的prototype属性指向Person的prototype对象
      17:           * Person的实例p的__proto__属性指向Preson的prototype对象
      18:           * Preson的prototype对象的constructor属性指向Person
      19:           */
      20:          console.log(p.constructor); // Person
      21:          console.log(p.__proto__ == Person.prototype); // true
      22:          console.log(p.__proto__.constructor == Person); // true  p.__proto__ 指向Person的prototype对象 Person的prototype对象constructor属性指向Person构造器
      23:   
      24:          //Object.create EcmaScript5 以上有
      25:          var Person2 = Object.create(p); //复制对象
      26:          console.log(Person2.name);// Lily
      27:          Person2.age = 20;
      28:          console.log(Person2.age);// 20
      29:          console.log(p.age);// 20
      30:   
      31:          var o1 = Object.create({x:1, y:2});
      32:          var o2 = Object.create(Object.prototype);
      33:          console.log(o2);
      34:          console.log(new Object());
  • 相关阅读:
    机器学习实战第7章——利用AdaBoost元算法提高分类性能
    js自定义事件的简单实现
    最完整的的判断使用的浏览器
    图片滚动图片的效果(不一样的实现思路)
    AspNetForum 论坛整改:添加了论坛联盟功能
    感叹之一:CSS样式
    ASPNETForums:如何创建多语言版本程序
    AspNetForum论坛整改:在论坛信息无法显示:浏览最多主题,回复最多的帖子……
    AspNetForum 论坛整改:添加显IP功能及IP所属地
    蓝牙抓包 WireShark 过滤方法
  • 原文地址:https://www.cnblogs.com/yuan001/p/3673920.html
Copyright © 2011-2022 走看看