• 创建对象的一种方式&一种继承机制(代码实例)


                /* 创建对象的一种方式:混合的构造函数/原型方式,
                 *用构造函数定义对象的所有非函数属性,用原型方式定义对象的函数属性(方法)
                 */
                function People(sname){
                    this.name = sname;
                }
                People.prototype.sayName = function(){
                    console.log(this.name);
                }
                /* 一种继承机制:
                 * 用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。
                 */
                function Student(sname,sage){
                    People.call(this,sname);
                    this.age = sage;
                }
                //临时构造函数模式(圣杯模式)
           var F = new Function(){} ;
           F.prototype = People.prototype ;
           Student.prototype = new F() ;
           Student.prototype.sayAge = function(){
                    console.log(this.age);
                };
          //实例
          var people1 = new People("jeff");
          people1.sayName(); //输出:jeff
          var student1 = new Student("john",30);
          student1.sayName(); //输出:john
          student1.sayAge(); //输出:30
  • 相关阅读:
    hibernate--could not initialize proxy
    20160509-hibernate--继承映射
    CF1111C Creative Snap
    CF1097D Makoto and a Blackboard
    CF1091D New Year and the Permutation Concatenation
    CF1096D Easy Problem
    CF1076E Vasya and a Tree
    CF1081C Colorful Bricks
    CF1081E Missing Numbers
    CF1093D Beautiful Graph
  • 原文地址:https://www.cnblogs.com/cag2050/p/5463935.html
Copyright © 2020-2023  润新知